MFC cstring usage summary 3

Source: Internet
Author: User

Use the buffer directly
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

9. cstring: Remove
Int cstring: Remove (tchar ch );
Return Value: the number of characters that are removed from the string. If the string is not changed, zero is returned.
Parameter: the character that CH needs to remove from a string.
Note: 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 .");
10. cstring: replace
Int Replace (tchar chold, tchar chnew );
Int Replace (maid, maid );
Return Value: the number of characters to be replaced. If the string is not changed, zero is returned.
Parameter: The character chold to be replaced by chnew.
Chnew is used to replace the chold character.
Lpszold is a pointer to a string that contains characters to be replaced by lpsznew.
Lpsznew is a pointer to a string that contains the characters to replace lpszold.
Note: 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.
Example:
// In the first example, old and new have the same length.
Cstring strzap ("C --");
Int n = strzap. Replace ('-', '+ ');
Assert (n = 2 );
Assert (strzap = "C ++ ");
// In the second example, old and new have different lengths.
Cstring strbang ("everybody likes ice hockey ");
N = strbang. Replace ("hockey", "Golf ");
Assert (n = 1 );
N = strbang. Replace ("likes", "plays ");
Assert (n = 1 );
N = strbang. Replace ("ice", null );
Assert (n = 1 );
Assert (strbang = "everybody plays golg ");
// Note that there is an extra space in your sentence.
// To remove this extra space, you can include it in the string to be replaced, for example, "ice ".

11. cstring: reversefind
Int reversefind (tchar ch) const;
Return Value: returns the index of the last character in the cstring object that matches the required character.
To the required characters,-1 is returned.
Parameter: the string to be searched.
Note: This member function searches for the last character matching a substring in this cstring object. This function
Similar to the runtime function strrchr.
Example:
// Cstring: reversefind example
Cstring S ("abcabc ");
Assert (S. reversefind ('B') = 4 );

12. cstring: Right
Cstring right (INT ncount) const;
Throw (cmemoryexception );
Return Value: The returned string is the last ncount character.
Cstring S (_ T ("abcdef "));
Assert (S. Right (2) = _ T ("Ef "));

13. cstring: setat
Void setat (INT nindex, tchar ch );
Note: The string can be understood as an array. setat is similar to []. Pay attention to the nindex range. If not, debugging errors may occur. Change the nindex position to Ch
Example:
Cstring S ("ABC ");
S. makereverse ();
Assert (S = "CBA ");

14. cstring: trimleft
Void trimleft ();
Void cstring: trimleft (tchar chtarget );
NOTE: If no parameter exists, remove the character (/n/T space, etc.) from the left to a non-such character. of course, you can also specify to delete those characters. if the specified parameter is a string, delete one of the characters.
/N linefeed
/T Tab character
Example 1:
Cstring STR = "/n/t ";
Str. trimleft ();
STR is "";
Example 2:
Cstring STR = "abbcadbabcadb ";
Str. trimleft ("AB ");
Result: "cadbabcadb"
Str. trimleft ("AC ");
Result: "bcadbabcadb"

15. cstring: trimright
Void trimright ();
Void cstring: trimright (tchar chtarget );
Void cstring: trimright (lpctstr lpsztargets );
Note: The usage is similar to the above.

16. cstring: Compare
Int compare (lpctstr lpsz) const;
Return Value: returns 0 if the string is the same, returns-1 if the value is smaller than lpsz, returns 1 if the value is greater than lpsz, and distinguishes characters in size.
Example:
Cstring S1 ("ABC ");
Cstring S2 ("Abd ");
Assert (s1.compare (S2) =-1 );
Assert (s1.compare ("Abe") =-1

 

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.