CString Constructor
CString ();
Example: CString csStr;
CString (const CString & stringSrc );
Example: CString csStr ("ABCDEF Chinese 123456 ");
CString csStr2 (csStr );
CString (TCHAR ch, int nRepeat = 1 );
Example: CString csStr ('A', 5 );
// CsStr = "aaaaa"
CString (maid, int nLength );
Example: CString csStr ("abcdef", 3 );
// CsStr = "abc"
CString (LPCWSTR lpsz );
Example: wchar_t s [] = L "abcdef ";
CString csStr (s );
// CsStr = L "abcdef"
CString (const unsigned char * psz );
Example: const unsigned char s [] = "abcdef ";
Const unsigned char * sp = s;
CString csStr (sp );
// CsStr = "abcdef"
CString (LPCSTR lpsz );
Example: CString csStr ("abcdef ");
// CsStr = "abcdef"
Int GetLength () const;
Returns the length of a string, excluding the trailing null characters.
Example: csStr = "ABCDEF Chinese 123456 ";
Printf ("% d", csStr. GetLength ());
// 16
Void MakeReverse ();
Reverse the string order
Example: csStr = "ABCDEF Chinese 123456 ";
CsStr. MakeReverse ();
Cout <csStr;
// CBA in article 654321
Void MakeUpper ();
Convert lowercase letters to uppercase letters
Example: csStr = "abcdef Chinese 123456 ";
CsStr. MakeUpper ();
Cout <csStr;
// ABCDEF Chinese 123456
Void MakeLower ();
Convert uppercase letters to lowercase letters
Example: csStr = "ABCDEF Chinese 123456 ";
CsStr. MakeLower ();
Cout <csStr;
// Abcdef Chinese 123456
Int Compare (LPCTSTR lpsz) const;
Compare two strings in case sensitive. If the value is equal, 0 is returned. If the value is greater than 1, 1 is returned. If the value is smaller than 1,-1 is returned.
Example: csStr = "abcdef Chinese 123456 ";
CsStr2 = "ABCDEF Chinese 123456 ";
Cout <csStr. CompareNoCase (csStr2 );
// 0
Int CompareNoCase (LPCTSTR lpsz) const;
Compare two strings in case-insensitive mode. If the values are equal, 0 is returned. If the values are greater than 1, 1 is returned. If the values are less than 1,-1 is returned.
Example: csStr = "abcdef Chinese 123456 ";
CsStr2 = "ABCDEF Chinese 123456 ";
Cout <csStr. CompareNoCase (csStr2 );
//-1
Int Delete (int nIndex, int nCount = 1)
Delete characters. Delete the nCount characters starting with the subscript nIndex.
Example: csStr = "ABCDEF ";
CsStr. Delete (2, 3 );
Cout <csStr;
// ABF
// When the nIndex is too large and exceeds the memory area of the object, the function does not perform any operations.
// When nIndex is negative, it is deleted from the first character.
// When the nCount value is too large and the characters to be deleted exceed the memory area of the object, unexpected results may occur.
// When nCount is a negative number, no operation is performed on the function.
Int Insert (int nIndex, TCHAR ch)
Int Insert (int nIndex, LPCTSTR pstr)
Insert a character or string at the position marked as nIndex. Returns the length of the inserted object.
Example: csStr = "abc ";
CsStr. Insert (2, 'x ');
Cout <csStr;
// Abxc
CsStr = "abc ";
CsStr. Insert (2, "xyz ");
Cout <csStr;
// Abxyzc
// When nIndex is negative, it is inserted at the beginning of the object
// When nIndex exceeds the end of the object, it is inserted at the end of the object.
Int Remove (TCHAR ch );
Removes specified characters from an object. Return the number of removed items.
Example: csStr = "aabbaacc ";
CsStr. Remove ('A ');
Cout <csStr;
// Bbcc
Int Replace (TCHAR chOld, TCHAR chNew );
Int Replace (maid, maid );
Replacement string
Example: csStr = "abcdef ";
CsStr. Replace ('A', 'x ');
Cout <csStr;
// Xbcdef
CsStr = "abcdef ";
CsStr. Replace ("abc", "xyz ");
Cout <csStr;
// Xyzdef
Void TrimLeft ();
Void TrimLeft (TCHAR chTarget );
Void TrimLeft (LPCTSTR lpszTargets );
Delete the character from the left. The deleted character matches chTarget or lpszTargets until the first unmatched character is deleted.
Example: csStr = "aaabaacdef ";
CsStr. TrimLeft ('A ');
Cout <csStr;
// Baacdef
CsStr = "aaabaacdef ";
CsStr. TrimLeft ("AB ");
Cout <csStr;
// Cdef
// Delete spaces when no parameters exist
Void TrimRight ();
Void TrimRight (TCHAR chTarget );
Void TrimRight (LPCTSTR lpszTargets );
Delete the character from the right. The deleted character matches chTarget or lpszTargets until the first unmatched character is deleted.
Example: csStr = "abcdeaafaaa ";
CsStr. TrimRight ('A ');
Cout <csStr;
// Abcdeaaf
CsStr = "abcdeaafaaa ";
CsStr. TrimRight ("fa ");
Cout <csStr;
// Abcde
// Delete spaces when no parameters exist
Void Empty ();
Clear
Example: csStr = "abcdef ";
CsStr. Empty ();
Printf ("% d", csStr. GetLength ());
// 0
BOOL IsEmpty () const;
Whether the test object is null. If it is null, zero is returned. If it is not null, non-zero is returned.
Example: csStr = "abc ";
Cout <csStr. IsEmpty ();
// 0;
CsStr. Empty ();
Cout <csStr. IsEmpty ();
// 1;
Int Find (TCHAR ch) const;
Int Find (LPCTSTR lpszSub) const;
Int Find (TCHAR ch, int nStart) const;
Int Find (LPCTSTR pstr, int nStart) const;
Query string. nStart is the start position. -1 is returned if no matching is found; otherwise, the start position of the returned string is returned.
Example: csStr = "abcdef ";
Cout <csStr. Find ('B ');
// 1
Cout <csStr. Find ("de ");
// 3
Cout <csStr. Find ('B', 3 );
//-1
Cout <csStr. Find ('B', 0 );
// 1
Cout <csStr. Find ("de", 4 );
//-1
Cout <csStr. Find ("de", 0 );
// 3
// If nStart exceeds the end of the object,-1 is returned.
// If nStart is negative,-1 is returned.
Int FindOneOf (LPCTSTR lpszCharSet) const;
Find the matching position of any character in the lpszCharSet in the CString object. -1 is returned if not found; otherwise, the start position of the returned string is returned.
Example: csStr = "abcdef ";
Cout <csStr. FindOneOf ("cxy ");
// 2
CString SpanExcluding (LPCTSTR lpszCharSet) const;
Returns the substring before the first character that matches any lpszCharSet in the object.
Example: csStr = "abcdef ";
Cout <csStr. SpanExcluding ("cf ");
// AB
CString SpanIncluding (LPCTSTR lpszCharSet) const;
Find any character that does not match any character in lpszCharSe from the object, and return the string before the first character that does not match
Example: csStr = "abcdef ";
Cout <csStr. SpanIncluding ("fdcba ");
// Abcd
Int ReverseFind (TCHAR ch) const;
Search for the first match from the back to the front. The subscript is returned when the match is found. -1 is returned when no result is found.
Example: csStr = "abba ";
Cout <csStr. ReverseFind ('A ');
// 3
Void Format (LPCTSTR lpszFormat ,...);
Void Format (UINT nFormatID ,...);
Format the object. It is used in the same way as the sprintf function in C language.
Example: csStr. Format ("% d", 13 );
Cout <csStr;
// 13
TCHAR GetAt (int nIndex) const;
Returns the nIndex character, which is used in the same way as the string [].
Example: csStr = "abcdef ";
Cout <csStr. GetAt (2 );
// C
// When nIndex is a negative number or exceeds the end of the object, unexpected results will occur.
Void SetAt (int nIndex, TCHAR ch );
Assign a value to the character whose subscript is nIndex.
Example: csStr = "abcdef ";
CsStr. SetAt (2, 'x ');
Cout <csStr;
// Abxdef
// When nIndex is a negative number or exceeds the end of the object, unexpected results will occur.
CString Left (int nCount) const;
Returns the string from the left.
Example: csStr = "abcdef ";
Cout <csStr. Left (3 );
// Abc
// If nCount is equal to 0, null is returned.
// If nCount is negative, null is returned.
// When nCount is greater than the object length, the returned value is the same as that of the object.
CString Right (int nCount) const;
Extract string from right
Example: csStr = "abcdef ";
Cout <csStr. Right (3 );
// Def
// If nCount is equal to 0, null is returned.
// If nCount is negative, null is returned.
// When nCount is greater than the object length, the returned value is the same as that of the object.
CString Mid (int nFirst) const;
CString Mid (int nFirst, int nCount) const;
Returns the string starting from the center.
Example: csStr = "abcdef ";
Cout <csStr. Mid (2 );
// Cdef
CsStr = "abcdef ";
Cout <csStr. Mid (2, 3 );
// Cde
// When nFirst is 0 and negative, it starts from the first character.
// If nFirst is equal to the end of the object, an empty string is returned.
// When nFirst exceeds the end of the object, unexpected results will occur.
// When nCount exceeds the end of the object, the string from nFirst to the end of the object is returned.
// When nCount is 0 and negative, an empty string is returned.
LPTSTR GetBuffer (int nMinBufLength );
Apply for a new space and return a pointer
Example: csStr = "abcde ";
LPTSTR pStr = csStr. GetBuffer (10 );
Strcpy (pStr, "12345 ");
CsStr. ReleaseBuffer ();
PStr = NULL;
Cout <csStr
// 12345
// After using GetBuffer, you must use ReleaseBuffer to update internal data of the object. Otherwise, unexpected results may occur.
Void ReleaseBuffer (int nNewLength =-1 );
After GetBuffer is used, ReleaseBuffer must be used to update internal data of the object.
Example: csStr = "abc ";
LPTSTR pStr = csStr. GetBuffer (10 );
Strcpy (pStr, "12345 ");
Cout <csStr. GetLength ();
// 3 (incorrect usage)
CsStr. ReleaseBuffer ();
Cout <csStr. GetLength ();
// 5 (correct)
PStr = NULL;
// Any method of the CString object should be called after ReleaseBuffer
LPTSTR GetBufferSetLength (int nNewLength );
Apply for a new space and return a pointer
Example: csStr = "abc ";
CsStr. GetBufferSetLength (20 );
Cout <csStr;
// Abc
Count <csStr. GetLength ();
// 20;
CsStr. ReleaseBuffer ();
Count <csStr. GetLength ();
// 3;
// You do not need to use ReleaseBuffer after using GetBufferSetLength.