The c ++ string is case-insensitive in C ++. Since the string object is not defined separately, it is more troublesome to perform string operations.
String case conversion is a common function. Today we will briefly summarize the Common conversion methods: Because ANSI and Unicode have differences 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 To uppercase Ansi: int toupper (int c); </a> Unicode: int towupper (wint_t c); MSDN: toupper, _ toupper, towupper, _ toupper_l, _ towupper_l to 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); // A "2. use the C ++ standard library function _ strlwr_s, _ strupr_s 」
Note: use safe string functions instead of _ strlwr. header file: string. h To lowercase: Ansi: errno_t _ strlwr_s (char * str, size_t numberOfElements); Unicode: errno_t _ wcslwr_s (wchar_t * str, size_t numberOfElements); note: numberOfElements requires the last NULL character length, I .e. numberOfElements = strlen (str) + 1; MSDN: http://msdn.microsoft.com/en-us/library/y889wzfw (VS.80 ). Aspx to uppercase: errno_t _ strupr_s (char * str, size_t numberOfElements); errno_t _ values (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 to 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 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 ;}after reading this example, I think it is worth the time to learn some STL things.