Transferred from: http://www.jizhuomi.com/software/228.html
Introduction to the CString class
CString class, as the common class of MFC, is well deserved. It can be said that as long as it is engaged in the development of MFC, Basic will encounter the use of CString class occasions. Because the use of strings is more common, and the CString class provides a convenient operation of the string, so it gives MFC developers a high level of development efficiency, has been welcomed by developers.
If you use VS2010, you may see CStringT, which is actually a template class that operates on variable-length strings. The CStringT template class has three instances:CString, CStringA, and CStringW, which provide operations for strings of TCHAR, Char, and wchar_t character types, respectively. The char type defines ANSI characters, the wchar_t type defines Unicode characters, and TCHAR depends on the configuration of the MFC Project's Properties dialog box properties->general-> Character the Set property, if this property is use Multi-Byte Character set, the TCHAR type defines the ANSI character, and if it is use Unicode Character set, The TCHAR type defines a Unicode character.
The operation of three string classes is the same, except that the character types are handled differently. Chicken Peck Rice to CString class for the explanation object.
string manipulation of the CString class
1. Constructors for the CString class
The CString class has many constructors, and here are just a few of the more common ones:
CString (const cstring& STRINGSRC);
Copies the contents of an already existing CString object STRINGSRC to the CString object. For example:
C + + code
CString str1 (_t ("www.jizhuomi.com")); // copy a constant string to str1
CString str2 (str1); //
CString (LPCTSTR lpch, int nlength);
Copies the first nlength characters in a string lpch to the CString object. For example:
C + + code
CString str (_t ("www.jizhuomi.com"),3); // The constructed string object content is "www"
CString (TCHAR ch, int nlength = 1);
The CString object constructed with this function will contain nlength of the repeating ch characters. For example:
C + + code
CString str (_t ('w'),3); // str is "www"
2. Case conversion and sequential conversion functions of the CString class
cstring& makelower ();
Converts all uppercase characters in a string to lowercase characters.
cstring& makeupper ();
Converts all lowercase characters in a string to uppercase characters.
cstring& makereverse ();
Reverses the order of all characters in the string.
For example:
C + + code
CString Str (_t ("jizhuomi")); Str. Makelower (); // str. Makeupper (); // str is "Jizhuomi" //
3. connection of CString objects
The connection of multiple CString objects can be implemented by overloading operator + and + =. For example:
C + + code
cstring str (_t ( " jizhuomi ")"; // str content is "Jizhuomi" str = _t ( " www. ") + str + _t ( " . "); // str is "Www.jizhuomi." str + = _t ( " COM "); // str is "www.jizhuomi.com"
4. comparison of CString objects
Comparisons of CString objects can be implemented by overloaded operators such as = =,! =, <, >, <=, >=, or by using the Compare and CompareNoCase member functions.
int Compare (PCXSTR psz) const;
Compares the CString object to the Psz string, returns 0 if it is equal, or less than 0 if it is less than psz, or a value greater than 0 if it is greater than psz.
int comparenocase (PCXSTR psz) const throw ();
This function is similar to the Compare function, except that it is case insensitive.
For example:
C + + code
cstring str1 = _t ( " jizhuomi " = _t ( " jizhuomi " ); if (str1 == str2) { // because STR1, str2 are not equal, do not execute the following code ...} if (0 = = str1. CompareNoCase (str2) { // because case-insensitive comparisons, Com The Parenocase function returns 0, so execute the following code ... }
5. extracting Operations for CString object strings
CString Left (int ncount) const;
Extracts a substring of ncount characters to the left of the string and returns a CString object that contains a copy of the substring.
CString Right (int ncount) const;
Extracts a substring of the ncount character to the right of the string and returns a CString object that contains a copy of the substring.
CString Mid (int ifirst,int ncount) const;
Extracts a substring of ncount characters starting at the index Ifirst position in the string, and returns a CString object that contains a copy of the substring.
CString Mid (int ifirst) const;
Extracts the substring in the string that starts at the index ifirst position until the end of the string, and returns a CString object that contains a copy of the substring.
For example:
C + + code
cstring str1 = _t ( " jizhuomi " = str1. Left (3 ); // str2 to "Jiz" str2 = str1. Right (2 ); // str2 to "Mi" str2 = str1. Mid (1 , 3 ); // str2 to "Izh" str2 = str1. Mid (5 ); // str2 to "Omi"
6. Find operations for CString object strings
int Find (PCXSTR pszsub,int istart=0) const throw ();
int Find (Xchar ch,int istart=0) const throw ();
Starts at the Istart index position of the CString object string, finds the position of the substring pszsub or the first occurrence of the character ch, and returns 1 if not found.
int findoneof (PCXSTR pszcharset) const throw ();
Finds any character in the pszCharSet string, returns the first occurrence of the position, and returns 1 if it is not found.
int reversefind (Xchar ch) const throw ();
Starts from the end of the string to find the specified character ch, returns its position, and returns 1 if it is not found. It is important to note that the index of the position is to be counted from the beginning, although it is looked up from behind.
C + + code
cstring str = _t ( " Jizhuomi " ); int nIndex1 = str. Find (_t ( " zh ")"; // int nIndex2 = str. Findoneof (_t ( " mui ")"; // int nIndex3 = str. Reversefind (_t ( ' i ")"; // The value of NINDEX3 is 7
7. substitution and deletion of CString class object strings
int Replace (PCXSTR pszold,pcxstr psznew);
Replaces the substring pszold in the CString object with the string psznew, returning the number of characters replaced.
int Replace (Xchar chold,xchar chnew);
Replaces the character Chold in the CString object with the character chnew, returning the number of characters replaced.
int Delete (int iindex,int ncount = 1);
Removes the ncount character from the string that begins with the iindex position, returning the length of the string after the delete operation.
int Remove (Xchar chremove);
Removes all characters specified by Chremove in the string, returning the number of characters deleted.
For example:
C + + code
CString str = _t ("Jizhuomi"); intN1 = str. Replace (_t ('I'), _t ('J'));//str is "JJZHUOMJ" and N1 is 2intN2 = str. Delete (1,2);//str is "JHUOMJ" and N2 is 6intN3 = str. Remove (_t ('J'));// str is "Huom" and N3 is 2
8. format string method for the CString class
Use the format member function of the CString class to format data types such as int, short, long, float, and double as string objects.
void __cdecl Format (pcxstr pszformat,[, argument] ...);
The parameter pszformat is the format control string, the parameter argument is optional, for the data to be formatted, generally each argument in Pszformat has corresponding sub-string representing its type, the argument of int should be "%d", Float type response should be "%f", and so on.
For example:
C + + code
CString str; int 1 ; float 2.3f ; Str. Format (_t ("a=%d,b=%f"), A, b); // str is "a=1,b=2.300000"
The content of the CString class is here, with a lot of usage, but still not all, you can view MSDN in-depth study.
VS2010-MFC (MFC Common class: CString Class)