Initialization of ①, CString class objects:
CString str; CString str1 (_t ("abc")); CString str2 = _t ("DEFG");
TCHAR szbuf[] = _t ("KKK"); CString STR3 (SZBUF); CString STR4 = szbuf; TCHAR *p = _t ("1k2"); TCHAR * converted to CString CString STR5 (p); CString STR6 = p; CString STR7 (STR1); CString str8 = STR7;
②, String basic operation:
Length
GetLength (); CString Str (_t ("abc")); int len = str. GetLength (); Len = = 3
is empty, that is, does not contain characters:
Empty the string:
Empty (); CString Str (_t ("abc")); BOOL mempty = str. IsEmpty (); Mempty = = FALSE Str. Empty (); Mempty = str. IsEmpty (); Mempty = = TRUE
Convert case:makeupper,makelower
Conversion Order:Makereverse
CString Str (_t ("ABC")); Str. Makeupper (); str = = ABC str. Makelower (); str = = ABC Str. Makereverse (); str = = CBA
The connection of the string:
+, + =
CString Str (_t ("abc")); str = _t ("de") + str + _t ("KP"); str = = Deabckp str + _t ("123"); str = = deabckp123 TCHAR szbuf[] = _t ("789"); str + = SZBUF; str = = deabckp123789
Comparison of strings:
= =,! =, (<, >, <=, >= not used),Compare(case sensitive),comparenocase(case insensitive)
CString str1 (_t ("abc")); CString str2 = _t ("ABc"); if (str1 = = str2) { MessageBox (_t ("str1 equals str2"));} else{ MessageBox (_t ("str1 not equal to str2"));}
③, String Lookup:
Find,reversefind,findoneof Three functions can implement the search operation of a string
Find finds the specified character or string from the specified position, returns its position, and finds no return-1;
CString Str (_t ("ABCDEFG")); int idx = str. Find (_t ("CDE"), 0); The value of IDX is 2;
Reversefind finds the specified character from the end of the string, returns its position, and finds no return-1, although it is looked up from the back, but the position is calculated from the beginning;
CString Str (_t ("ABCDEFG")); int idx = str. Reversefind (' e '); The value of IDX is 4;
Findoneof finds any character in the given string in the parameter, returns the first occurrence of the position, and cannot find the return-1;
CString Str (_t ("ABCABCD")); int idx = str. Findoneof (_t ("CBD")); The value of IDX is 1;
④, substitution and deletion of strings:
Replace replaces the specified character or string in the CString object, returns the number of replacements, and returns 0 without matching characters;
Remove deletes the specified character in the CString object, returns the number of deleted characters, and deletes it when there are multiple;
CString Str (_t ("ABCDABCB")); int num = str. Remove (' B '); str = = ACDAC, num = = 3
Delete deletes the character at the specified position in the CString object, returning the processed string length;
CString Str (_t ("ABCD")); int num = str. Delete (1, 3); str = = A, num = = 1
⑤, String extraction:
left,mid, right three functions are implemented to extract the string from the CString object from the top, the middle, and the other.
CString Str (_t ("ABCD")); CString strresult = str. Left (2); strresult = = AB strresult = str. Mid (1); strresult = = BCD Strresult = str. Mid (0, 2); strresult = = AB strresult = str. Right (2); strresult = = CD
⑥, single-character modification:
GetAt,SetAt can get and modify a single TCHAR type character in a CString object, and the [] operator can also get a single character in a CString object, but is read-only , cannot be modified;
CString Str (_t ("ABCD")); Str. SetAt (0, ' k '); str = = KBCK TCHAR ch = str. GetAt (2); ch = = C
Conversion of ⑦, other types, and CString object types:
Formatted string: The Format method, which implements conversions from types such as int, long, TCHAR, TCHAR * to CString type;
int num = 6; CString str; Str. Format (_t ("%d"), num);
Conversion of CString types to numeric types such as int, TCHAR * Type:
TCHAR *pszbuf = str. GetBuffer (); Str. ReleaseBuffer (); TCHAR *p = (LPTSTR) (LPCTSTR) str; CString str1 (_t ("123")); int num = _ttoi (STR1);
Ansi and Unicode conversions for ⑧, CString objects:
Also here is an implicit method for converting from Ansi to Unicode:
The current engineering environment is Unicode CString STR;STR = "abc"; char *p = "DEFG"; str = p;
⑨, ※※※cstring the number of bytes occupied by the object string:
CString str = _t ("abc");
The wrong approach: sizeof (CString), sizeof (STR)
Correct method of finding: Str. GetLength () *sizeof (TCHAR)
⑩, as a TCHAR * type, ensure that sufficient space is applied, such as using the getmodulefilename function;
CString Common operations