Not finalized, please refer to the name.
First define the variables that will be used later:
CSTRING STR1, STR2, STR3;
Summary:
MFC's encapsulation of the CString class is not as good as std :: string, but it is also good, the function is strong enough, it is still very considerate. Its basic characteristics are:
☆ The CString class has no base class.
☆ The relationship between CSTRING class and LPCTSTR: MSDN said "CString Objects Follow" Value Semantics. "Think of a cstring Object As an actual string, not as a pointer to a string." Although it is used to consider using it. The way the member function is used as a normal C-Style string to use it. You can use LPCTSTR in the constructor:
CSTRING STR ("Hello World!");
can:
Str1 = str2; or str1 = "hello";
You can also:
STR3 = STR1 STR2; or STR3 = STR1 "Hello";
Of course, you can:
STR1 = STR2; or Str1 = "Hello";
In fact, these operators can be used between CString objects or between CSTRING and LPCTSTR objects:
==,! =, <,>, <=,> =
Naturally, forced CString objects to convert to the LPCTSTR type should also be rational:
LPCTSTSTSTSTSTSTSTR) STR1;
☆ CString supports Unicode and multi-byte character sets (MBCs). Because it is itself based on tchar - Of course, you don't forget to define the encoding method, such as: #define _unicode.
☆ CSTRING supports the reference count. You can disable / enable reference counts through its member functions LockBuffE / and UNLOCKBUFFER.
◆
For the definition of the member function of the CString class, the return value and other forms are not described herein, please refer to:
http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_cstring_class_members.asp
Related links.
Common functions and examples:
☆ Change sensitivity:
CSTRING :: Makeupper and CString :: Makelower two member functions (without parameters) make the entire string into large / lowercase letters.
Example: str1 = "hello";
Str1.makeupper ();
AFXDUMP << str1; // output result is "hello";
☆ reverse: void cstring :: makereverse ();
☆ Read the string from the.rc file:
The cstring :: loadString function reads the string of the incoming string resource ID into the CString object. Returns a non-zero value if successful.
Bool Bresult = loading 4tring; IDS_FileNotfound;
☆ Subsidence operation
→ Remove the left space of the string: str1.trimleft ();
→ Remove the string of skewers: str1.trimright ();
→ Get the specified position character: char a = str1.getat (3); the corresponding CString :: setat function, modify the specified location character. → Delete all specified characters in the string:
Str1 = "Hello Test";
Str1.Remove ('t');
AFXDUMP << str1; // Output "Hello ES";
→ Delete a substring of the specified length of the specified location:
Str1 = "Hello Test";
Str1.delete (3, 2); // The first parameter is index (starting from zero)
// Second parameter is the length
AFXDUMP << STR1; // Output "Hel Test";
→ Empty the content of the object:
Void cstring :: Empty ();
→ Find substrings:
※ The cstring :: Find function has four overloads, which can be searched for characters and strings, and you can also specify the starting position of the search and return the first lookup INDEX.
INT FIND (TCHAR CH) Const;
INT FIND (LPCTSTR LPSZSUB) Const;
INT FIND (TCHAR CH, INT NSTART) Const;
INT FIND (LPCTSTSTR PSTR, INT NSTART) Const;
※ cstring :: ReverseFind is the index of the last matching character in the string, and looks forward to the Find function, but unfortunately there is only one overload:
INT Reversefind (Tchar CH) Const;
※ CString :: FINDONEOF is the first INDEX with any of the specified strings. (It seems to be a bit winding, look at the example and understand)
Str1 = "Hello Test";
INT j = str1.find ("el");
AFXDUMP << "j =" << j << "/ n";
INT K = str1.find ('e', 3);
AFXDUMP << "k =" << k << "/ n";
INT L = str1.reversefind ('t');
AFXDUMP << "l =" << l << "/ n";
INT m = str1.reversefind ('t');
AFXDUMP << "m =" << m << "/ n";
INT N = STR1. Findoneof ("stuv");
AFXDUMP << "n =" << n << "/ n";
Output results:
J = 1
K = 7
L = 9
m = 9
N = 6
→ String cut off: cstring :: Left, cstring :: Right function is only one parameter, and a CString object is returned, and the role is to intercept the substrings of the left / right specified length. CSTRING :: MID function The first parameter specified position, the second parameter specifies the length. These are commonly used functions, they don't write examples.
→ get buffer
Often some people asked the CString object and the conversion problem of char *, in addition to the previous mandatory conversion, use this
LPTSTSTSTSTSTSTSTSTH (INT NNEWLENGT); use the returned pointer to directly modify the contents of the CString object, but there should be note that if the specified length is shorter than the original CString length (truncated) Please remember to make '/ 0'
Second, please remember ReleaseBuffer before calling the CString object, maybe it doesn't seem to have an error, but it is not the root cause of a big hidden danger.
→ cstring :: spanexcluding function
I used to answer a problem with the String of the CString object. Now I think about it. If you use this function, how convenient will be made. Function prototype:
Cstring spanexcluding (lpctstr lpszcharset) const;
It finds the first character that matches any match in the CString object and returns a CString object, which is the original object from the starting position to find the previous character of the character. This will be very convenient when separating the substrings separated by a split (comma space):
Str1 = "Hello Test";
Str2 = str1.spanexcluding (",");
AFXDUMP << STR2; // Output "Hello"
At the same time, there is also a very convenient function: cstring :: spanincluding, function prototype:
CString Spanincluding (LPCTSTR LPSZCHARSET) Const;
It returns a few characters in the object, which must be in the lpszcharst:
Str1 = "Hello Test";
Str2 = str1.spanincluding ("abcdefghijk");
AFXDUMP << STR2; // Output "H"
→ Insert Subrout: Use cstring :: INSERT to insert characters or strings to the specified location
Str1 = "Hello Test";
Str1.insert (2, "abcd");
AFXDUMP << Str1; // Output "HeabCDLLO Test"
→ Replace: cstring :: Replace The role is to replace all matching metals in the original object / substring. There are two heavy load prototypes:
INT Replace (Tchar Chold, Tchar Chnew);
INT Replace (LPCTSTR LPSZOLD, LPCTSTR LPSZNEW);
☆ Attributes of CString objects: These are very common, brief description
INT getLength () const; // Get the length of buffer
BOOL ISEMPTY () const; // Judging whether the content of the CString object is empty
INT COMPARE (LPCTSTR LPSZ) Const; / / Compare ASCII
INT COMPARENOCASE (LPCTSTR LPSZ) Const; // Compares with LPSZ by ASCII code, ignore cases
CString :: format / * used to format the object. I don't want to put the object itself in the parameter of the Format function. * /