Overview: CString is a class for handling strings provided in MFC and is a very useful data type.
It greatly simplifies many operations in MFC, making MFC a lot more convenient when doing string operations.
Anyway, there are many special skills in using CString, especially for programmers coming out of pure C background, it is a bit difficult to learn.
I. Introduction
StringCString is located in the header file afx.h, this article will discuss these techniques.
Refer to Baidu Encyclopedia and intercept some of the useful content for me. This article includes the following:
<1> CString object connection
<2> Format string (including conversion of int to CString)
<3> Member functions of CString class
<4> CString converted to int
<5> Mutual conversion between CString and char *
Char * to CString
CString into one of char *: use LPCTSTR
CString into char * two: Use the GetBuffer method of the CString object
CString into char * three: the interface with the control
Object connection
The CString class overloads the + operator, so CString objects can be directly connected using the + sign. Instead of using the strcat function
View Code
Note: In MFC programming, in order to be compatible with Unicode encoding, try to use the _T () macro for strings.
Format strings
You can use the CString Format method to format a CString object with a C-style string instead of using the sprintf function
View Code
Fourth, member functions
1. Constructor
StringCString has many constructors. Here are just a few common ones:
注意 (note that the variable names of the function prototype are all my own, the name in the source code may not be this name, but it does not affect)
(Other class object reference name uses other, the number of characters uses n)
<1> Initialize CString object with existing CString object
原型 Function prototype: CString (const CString & other);
<2> CString objects can be initialized with constant strings
Function prototype: CString (const LPCTSTR other); // This prototype is my guess
Example: CString str (_T ("Hello, World!"));
<3> Copy the first n characters in the string other to this CString object
Function prototype: CString (LPCTSTR other, int n);
Example: CString str (_T ("Hello, World! This is redundant \ n"), 12);
<4> Initialize CString object with n letters ch
Function prototype: CString (TCHAR ch, int n = 1);
Example: CString str ('6', 6); / str is initialized to 6 6, which is 666666
View Code
Note: the = operator is used to call the corresponding type of constructor during initialization, not the overloaded = operator,
In addition, some constructors cannot be written in the form of =, such as initializing a CString object with n ch, initializing a class object with the first n characters of a string, etc
2. Case conversion and order conversion functions of the CString class
<1> convert all uppercase characters in the string to lowercase characters
Function prototype: CString & MakeLower ();
<2> Convert all lowercase characters in the string to uppercase characters
Function prototype: CString & MakeUpper ();
<3> Reverse the order of all characters in the string
Function prototype: CString & MakeReverse ()
<4> To do the corresponding operation, use the class object to call the corresponding function, then the value of the object will be modified
View Code
3.CString object connection
The connection of multiple CString objects can be achieved through the overloaded operators +, + =.
For details, please refer to the above (2.Object connection)
View Code
4. Comparison of CString objects
CString objects can be compared by using overload operators such as ==,! =, <;,> ;, <=,> =, or by using the Compare and CompareNoCase member functions.
<1> ==,! =, <,>, <=,> = Are all compared according to the ASCII value size (the lexicographic order of the strings),
The return value is 0 or 1, which indicates that the comparison operator used is valid.
<2> Compare function is similar to strcmp function, it returns 0 for equality,
Parameters less than the return value will return a number less than 0, and parameters greater than the return value will return a number greater than 0.
<3> CompareNoCase is similar to Compare, except it is not case sensitive.
View Code
5.CString object string extraction operation
<1> Extracts a substring of nCount characters to the left of the string, and returns a CString object containing a copy of this substring
Function prototype: CString Left (int nCount) const;
<2> Extract the substring of nCount characters to the right of the string, and return a CString object containing a copy of this substring
Function prototype: CString Right (int nCount) const;
<3> Extracts a substring consisting of nCount characters starting from the index iFirst position in the string, and returns a CString object containing a copy of this substring
Function prototype: CString Mid (int iFirst, int nCount) const;
Extract the substring from the string starting at the index iFirst position to the end of the string, and return a CString object containing a copy of this substring
Function prototype: CString Mid (int iFirst) const;
View Code
6.CString object string search operation
<1> starts at the iStart index position of the CString object string, finds the position where the substring pszSub or the character ch first appears, and returns -1 if it is not found
Int Find (PCXSTR pszSub, int iStart = 0) const throw (); // Find the first occurrence of the substring
Int Find (XCHAR ch, int iStart = 0) const throw (); // Find the first occurrence of a character
<2> Find any character in pszCharSet string, return the first occurrence position, -1 if not found
Int FindOneOf (PCXSTR pszCharSet) const throw (); // Find the first occurrence of any character in the given string in the original string
<3> Find the specified character ch from the end of the string and return its position, or -1 if it cannot find it. It should be noted here that although the search is performed from the back to the front, the index of the position must be counted from the beginning.
Int ReverseFind (XCHAR ch) const throw ();
View Code
7. Replacement and deletion of CString class object strings
<1> Replace the substring pszOld in the CString object with the string pszNew, and return the number of replaced strings
Int Replace (PCXSTR pszOld, PCXSTR pszNew); // Replace substring
View Code
<2> Replace the character chOld in the CString object with the character chNew, and return the number of characters replaced
Int Replace (XCHAR chOld, XCHAR chNew); // Replace characters
View Code
<3> Delete the nCount characters starting from the iIndex position in the string, and return the length of the string after the delete operation
Int Delete (int iIndex, int nCount = 1);
View Code
<4> Delete all characters specified by chRemove in the string, and return the number of deleted characters.
Int Remove (XCHAR chRemove);
View Code
8. Format string method of CString class
<1> Use the Format member function of the CString class to format int, short, long, float, double and other data types as string objects.
Function prototype: void __cdecl Format (PCXSTR pszFormat, [, argument] ...);
<2> The parameter pszFormat is a format control string; the argument argument is optional, which is the data to be formatted.
In general, each argument has a corresponding substring indicating its type in pszFormat. An int argument should be "% d", a float argument should be "% f", etc.
View Code
9. When doing the principle of compilation, you need to use the Insert method. I specifically checked it and hereby add:
<1> function prototype
Int Insert (int iIndex, PCXSTR psz);
Int Insert (int iIndex, XCHAR ch);
<2> Parameter description
IIndex to insert a character or string before the position
Psz string to insert
Ch character to insert
<3> returns the length of the new CString
<4> The iIndex parameter identifies the mobile to make room. The first character of a character or substring.
If nIndex is zero, it occurs before the entire string. If nIndex is higher than the length of the string, the function will concatenate ch or psz with the current string provided by the new material
View Code
Five, int type
最 The easiest way to convert CString data to integer type is to use standard string to integer conversion routines
<1> Although usually you suspect that using the _atoi () function is a good choice, it is rarely the right choice
<2> If you plan to use Unicode characters, you should use _ttoi (), which is compiled into _atoi () in the ANSI encoding system and _wtoi () in the Unicode encoding system.
Note: the first parameter is the string to be converted, the second parameter is the ending position, and is generally set to NULL, and the third is the parameter to be converted
<3> You can also consider using _tcstoul () or _tcstol (), they can convert strings to long integers in any base (such as binary, octal, decimal or hexadecimal)
The difference is that the transformed data is unsigned, while the latter is the opposite.
View Code
Mutual conversion between CString and char *
1.char * to CString
View Code
2.CString to char *
<1> cast to LPCTSTR
A. This is a slightly rigid conversion. There is still a lot of confusion in the understanding of the "correct" approach.
There are many correct usage methods, but there may be as many incorrect usage methods as there are correct usage methods.
We first need to understand that CString is a very special C ++ object, which contains three values:
A pointer to a data buffer, a valid character count in the buffer, and a buffer length.
Number of valid characters
Please read the Chinese version for the details.
Contact: Manager Xu
Phone: 13907330718
Tel: 0731-22222718
Email: hniatcom@163.com
Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province