Cstring common methods

Source: Internet
Author: User
Tags uppercase character
Cstring common methods

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
 
Cstring: getlength
Int getlength () const;
Return Value
Returns the byte count in the string.
Description
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 );
Returns the modified length. nindex is the inserted index number of a character (or string ).
Cstring STR ("hockeybest ");
Int n = Str. insert (6, "is ");
Assert (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 be extracted, and 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 );
Parameters
Nnewlength
The new length of this string, expressed by the number of characters, is not counted as an empty character at the end. If the word
If the string ends with an empty character, the default value-1 of the parameter sets the cstring size
The current length of the string.
Description
Use releasebuffer to end the use of the buffer allocated by getbuffer. If you know
The nnewlength parameter can be omitted if the string in the punch area ends with a null character. If the character
If the string does not end with a null character, you can use nnewlength to specify the length of the string. Before calling
After releasebuffer or other cstring operations, 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 zone
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
Returns the number of characters removed from the string. If the string is not changed, zero is returned.
Parameters
Ch
The character to be removed from a string.
Description
This member function is used to remove the CH instance from the string. This character is case sensitive.
.
Example
// Remove the lower-case 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
The 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.