In C ++, since the string object is not defined separately, string operations are more troublesome.
String case conversion is a common function. Today we will summarize the Common conversion methods:
Because ANSI and Unicode are different in function names, they are all listed, but I mainly use Unicode.
[1. Use the C standard library function toupper and tolower]
Header file: Under cctype C: ctype. h
Convert to uppercase
ANSI: int toupper (int c); </a>
UNICODE: int towupper (wint_t C );
Msdn: toupper, _ toupper, towupper, _ toupper_l, _ towupper_l
Lowercase:
Int tolower (
Int C
);
Int towlower (
Wint_t C
);
Msdn: tolower
Defect: Only one character can be converted
Example:
Wchar wch = 'a ';
Wch = towupper (wch); //
[2. Use the C ++ standard library function _ strlwr_s, _ strupr_s]
Note: use safe string functions instead of _ strlwr.
Header file: String. h
Lowercase:
ANSI:
Errno_t _ strlwr_s (
Char * STR,
Size_t numberofelements
);
UNICODE:
Errno_t _ wcslwr_s (
Wchar_t * STR,
Size_t numberofelements
);
Note: numberofelements must contain the last null character, that is, numberofelements = strlen (STR) + 1;
Msdn: http://msdn.microsoft.com/en-us/library/y889wzfw (vs.80). aspx
Convert to uppercase:
Errno_t _ strupr_s (
Char * STR,
Size_t numberofelements
);
Errno_t _ wcsupr_s (
Wchar_t * STR,
Size_t numberofelements
);
Msdn: http://msdn.microsoft.com/en-us/library/sae941fh (vs.80). aspx
Example:
Wchar widestr [] = l "ABC ";
_ Wcslwr_s (widestr, wcslen (widestr) + 1); // ABC
_ Wcsupr_s (widestr, wcslen (widestr) + 1); // ABC
[3.std:: String Conversion case]
Unfortunately, STD: String does not support case-sensitivity conversion. Therefore, you can only use the Transform in STL to combine toupper/tolower.
Header file: String, cctype, Algorithm
Lowercase
Transform (Str. Begin (), str. End (), str. Begin (), tolower );
Transform (wstr. Begin (), wstr. End (), wstr. Begin (), towlower );
Convert to uppercase
Transform (S. Begin (), S. End (), S. Begin (), toupper );
Transform (wstr. Begin (), wstr. End (), wstr. Begin (), towupper );
Example:
Wstring wstr = l "ABC ";
Transform (wstr. Begin (), wstr. End (), wstr. Begin (), towupper );
[4. In the boost library, string_algorithm provides the case-sensitive conversion functions to_lower and to_upper]
Example:
# Include <boost/algorithm/string. HPP>
Using namespace STD;
Using namespace boost;
Wstring wstr = l "ABC ";
Boost: to_lower (wstr); // ABC
========================================================== ======================================
Complete example
**
* @ File test. cpp
* @ Brief case-sensitive Conversion
* @ Author greenerycn@gmail.com
* @ Date 2009-7-1
*/
# Include "stdafx. H"
# Include <cstring>
# Include <windows. h>
# Include <cctype>
# Include <algorithm>
# Include "Boost \ algorithm \ string. HPP"
Using namespace STD;
Int wmain (INT argc, wchar * argv [])
{
Char CH = 'a ';
Ch = toupper (CH );
Wchar wch = 'a ';
Wch = towupper (wch );
Wchar widestr [] = l "ABC ";
_ Wcslwr_s (widestr, wcslen (widestr) + 1 );
_ Wcsupr_s (widestr, wcslen (widestr) + 1 );
Wstring wstr = l "ABC ";
Transform (wstr. Begin (), wstr. End (), wstr. Begin (), towupper );
Boost: to_lower (wstr );
Return 0;
}