In VC, cstring is one of our most commonly used classes. We feel familiar with it, but do you know all its usage? Let's take a look at the system and read this article carefully. Start as follows: 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; If the return value does not match,-1 is returned; the index starts with 0. nstar indicates that the index starts with the nstart character, 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; If the return value does not match,-1 is returned. The index starts with 0. note :: returns the first index value of this string that also contains characters in lpszcharset and starts from scratch. 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 cstring: getlength int getlength () const; return value return the byte count in the string. note This member function is used to obtain the byte count in the cstring object. This count does not include the ending null characters. for multi-byte character sets (MBCS), getlength is counted by every eight characters. That is, the start and end bytes in a multi-byte character are counted as two bytes. example the following example shows how to use cstring: getlength. // cstring: getlength example cstring S ("abcdef"); assert (S. getlength () = 6); cstring: insert int insert (INT nindex, tchar ch); int insert (INT nindex, lpctstr pstr ); return the modified length. The nindex is the inserted Index Number of the character (or string). cstring STR ("hockeybest "); int n = Str. insert (6, "is"); ASSE RT (n = Str. getlength (); printf ("1: % s \ n", (lpctstr) Str); N = Str. insert (6, ''); assert (n = Str. getlength (); printf ("2: % s \ n", (lpctstr) Str); N = Str. insert (555, '1'); assert (n = Str. getlength (); printf ("3: % s \ n", (lpctstr) Str); output 1. hockeyis best 2. hockey is best 3. hockey is best! Cstring: isempty Bool isempty () const; Return Value If the length of the cstring object is 0, a non-zero value is returned; otherwise, 0 is returned. Description This member function is used to test whether a cstring object is null. Example The following example shows how to use cstring: isempty. // Cstring: isempty example Cstring S; Assert (S. isempty ()); See cstring: getlength Cstring: left Cstring left (INT ncount) const; Throw (cmemoryexception ); The returned string is the first ncount character. Example Cstring S (_ T ("abcdef ")); Assert (S. Left (2) = _ T ("AB ")); Cstring: loadstring Bool loadstring (uint NID ); Throw (cmemoryexception ); Return Value If the resource is successfully loaded, a non-zero value is returned; otherwise, 0 is returned. NID: A Windows string resource ID. This member function is used to read a Windows string resource identified by NID and put it into an existing cstring object. Example The following example shows how to use cstring: loadstring. // Cstring: loadstring example # Define ids_filenotfound 1 Cstring S; If (! S. loadstring (ids_filenotfound )) Cstring: makelower Void makelower (); Change lowercase letters cstring: makereverse void makereverse (); character inversion cstring: makeupper void makeupper (); change the uppercase character cstring :: mid cstring mid (INT nfirst) const; cstring mid (INT nfirst, int ncount) const; ncount indicates the number of characters to extract, nfirst indicates the start index location to be extracted example cstring S (_ T ("abcdef"); assert (S. mid (2, 3) = _ T ("CDE"); cstring: releasebuffer void releasebuffer (INT nnewlength =-1 ); parameter nnewlength This string Is a new length expressed by the number of characters. It is not counted as an empty character at the end. If the string ends with a null character, the default value of-1 of the parameter sets the cstring size to the current length of the string. note releasebuffer is used to end the use of the buffer allocated by getbuffer. If you know that the string in the slow section ends with an empty character, you can omit the nnewlength parameter. If the character string does not end with a null character, you can use nnewlength to specify the length of the string. After the releasebuffer or other cstring operations are called, the address returned by getbuffer is invalid. example the following example shows how to use cstring: releasebuffer. // cstring: releasebuffer example cstring s; S = "ABC"; lptstr P = S. getbuffer (1024); strcpy (P, "ABC"); // directly use this buffer assert (S. getlength () = 3); // String Length = 3 S. releasebuffer (); // Release excess memory. P is invalid now. assert (S. getlength () = 3); // The length is still 3 cstring: Remove int cstring: Remove (tchar ch); return value return the number of characters removed from the string. If the string is not changed, zero is returned. parameter CH the character to be removed from a string. note This member function is used to remove the CH instance from the string. The comparison with this character is case sensitive . example // remove the lowercase letter 'C' from a sentence: cstring STR ("this is a test. "); int n = Str. remove ('T'); assert (n = 2); assert (STR = "This is a es. "); Cstring: replace Int Replace (tchar chold, tchar chnew ); Int Replace (maid, maid ); Return Value Returns the number of replaced characters. If the string is not changed, zero is returned. Parameters Chold Character to be replaced by chnew Chnew The character to be used to replace chold. Lpszold A pointer to a string that contains characters to be replaced by lpsznew. Lpsznew A pointer to a string that contains the characters used to replace lpszold. Description This member function replaces another character with one character. The first prototype of the function is chnew in the string. Replace chold on site. The second original form of the function replaces the string specified by lpszold with the string specified by lpsznew . After replacement, the string may increase or decrease; that is because the length of lpsznew and lpszold It does not need to be equal. Both versions are case-sensitive. The full text is now complete. |