1. cstring header files
# Include <afx. h>
2. cstring output
Cstring temp = "Hello! ";
Cout <(lpctstr) temp <Endl;
3. cstring usage
Cstring: Compare
Int compare (lpctstr lpsz) const;
Returns 0 if the return value is the same as the string.
If the value is less than lpsz,-1 is returned.
If the value is greater than lpsz, 1 is returned.
Characters of different sizes
Cstring S1 ("ABC ");
Cstring S2 ("Abd ");
Assert (s1.compare (S2) =-1 );
Assert (s1.compare ("Abe") =-1 );
Cstring: comparenocase
Int comparenocase (lpctstr lpsz) const;
Returns 0 if the return value is the same as the string.
If the value is less than lpsz,-1 is returned.
If the value is greater than lpsz, 1 is returned.
Not distinguished by characters
Cstring: collate
Int collate (lpctstr lpsz) const;
Same as cstring: Compare
Cstring: collatenocase
Int collatenocase (lpctstr lpsz) const;
Same as cstring: comparenocase
Cstring: cstring
Cstring ();
Cstring (const cstring & stringsrc );
Cstring (tchar CH, int nrepeat = 1 );
Cstring (maid, int nlength );
Cstring (const unsigned char * psz );
Cstring (lpcwstr lpsz );
Cstring (lpcstr lpsz );
The example is the easiest to describe.
Cstring S1;
Cstring S2 ("cat ");
Cstring S3 = S2;
Cstring S4 (s2 + "" + S3 );
Cstring s5 ('x'); // S5 = "X"
Cstring S6 ('x', 6); // S6 = "xxxxxx"
Cstring S7 (lpcstr) id_file_new); // S7 = "Create a new document"
Cstring city = "Philadelphia ";
Cstring: Delete
Int Delete (INT nindex, int ncount = 1 );
The returned value is the length of the string before it is deleted.
Nindex is the first character to be deleted, and ncount is the number of characters to be deleted at a time. According to the results obtained from our experiment: When ncount> is about to delete the maximum length of the string (getcount ()-nindex), an error occurs. When ncount is too large and there are not enough characters to delete it, this function is not executed.
Example
Cstring str1, str2, str3;
Char;
Str1 = "nihao ";
Str2 = "nihao ";
Int X;
// Int I = (str1 = str2 );
Str1.delete (2, 3 );
If ncount (3)> getcount ()-nindex (5-2), an error is returned.
Cstring: empty
Void empty ();
No return value clearing operation;
Example
Cstring S ("ABC ");
S. Empty ();
Assert (S. getlength () = 0 );
Cstring: Find
Int find (tchar ch) const;
Int find (lpctstr lpszsub) const;
Int find (tchar CH, int nstart) const;
Int find (lpctstr lpszsub, int nstart) const;
-1 is returned if the return value does not match. The index starts with 0.
Nstar indicates that the search starts with the index value nstart,
It is a string that contains the indexed nstart characters.
Example
Cstring S ("abcdef ");
Assert (S. Find ('C') = 2 );
Assert (S. Find ("de") = 3 );
Cstring STR ("the stars are aligned ");
Ing n = Str. Find ('E', 5 );
Assert (n = 12)
Cstring: findoneof
Int findoneof (lpctstr lpszcharset) const;
-1 is returned if the return value does not match. The index starts with 0.
Note: The first index value in this string that contains characters and starts from scratch in lpszcharset is returned.
Example
Cstring S ("abcdef ");
Assert (S. findoneof ("XD") = 3); // 'D' is first match.
Cstring: Format
Void format (lpctstr lpszformat ,...);
Void format (uint nformatid ,...);
Lpszformat: A format control string
Nformatid string identifier
Example
Cstring STR;
Str. Format ("% d", 13 );
STR is 13
Cstring: getat
Tchar getat (INT nindex) const;
Returns the nindex character. You can regard the string as an array. getat is similar to []. Note the nindex range. If not, a debugging error occurs.
Cstring: getbuffer
Lptstr getbuffer (INT nminbuflength );
Return Value
An lptstr pointer pointing to the buffer zone of the object (ending with a null character.
Parameters
Nminbuflength
The minimum size of the character buffer, expressed by the number of characters. This value does not include the space of an ending null character.
Description
This member function returns a pointer to the internal character buffer of the cstring object. The returned lptstr is not a const, so you can directly modify the cstring content. If you use a pointer returned by getbuffer to change the content of a string, you must call the releasebuffer function before using other cstring member functions.
After calling releasebuffer, the address returned by getbuffer may be invalid, because other cstring operations may cause the cstring buffer to be reallocated. If you do not change the length of the cstring, the buffer will not be reassigned. When the cstring object is destroyed, its buffer memory is automatically released.
Note: If you know the length of a string, you should not add null characters at the end. However, when you use releasebuffer to release the buffer, you must specify the final string length. If you add an empty character at the end, you should pass-1 to the releasebuffer length parameter, and releasebuffer will execute strlen on the buffer to determine its length.
The following example shows how to use cstring: getbuffer.
// Cstring: getbuffer example
Cstring S ("ABCD ");
# Ifdef _ debug
Afxdump <"cstring s" <S <"/N ";
# Endif
Lptstr P = S. getbuffer (10 );
Strcpy (P, "hello"); // directly access the cstring object.
S. releasebuffer ();
# Ifdef _ debug
Afxdump <"cstring s" <S <"/N ";
# Endif