Application of VC cstring processing functions

Source: Internet
Author: User
Tags uppercase character

Many functions in cstring are similar to those in string processing in VB.

1. 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.

Note: 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

 

2. cstring: left

Cstring left (INT ncount) const;

Throw (cmemoryexception );

Return Value: The returned string is the first ncount character.

Example:

Cstring S (_ T ("abcdef "));

Assert (S. Left (2) = _ T ("AB "));

 

3. 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.

Note: 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 ))

 

4. cstring: makelower

Void makelower (); // change the lowercase character

 

5. cstring: makereverse

Void makereverse (); // character Inversion

 

6. cstring: makeupper

Void makeupper (); // change the uppercase character

 

7. 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 "));

 

8. cstring: releasebuffer

Void releasebuffer (INT nnewlength =-1 );

Parameter: 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.

Note:

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

 

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

17. cstring: comparenocase

Int comparenocase (lpctstr lpsz) const;

Return Value: returns 0, less than lpsz returns-1, greater than lpsz returns 1, not size characters

 

18. cstring: collate

Int collate (lpctstr lpsz) const;

Same as cstring: Compare

 

19. cstring: collatenocase

Int collatenocase (lpctstr lpsz) const;

Same as cstring: comparenocase

 

20. cstring: cstring // Constructor

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 );

Example:

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 ";

 

21. cstring: Delete

Int Delete (INT nindex, int ncount = 1 );

Returned value: 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.

 

22. cstring: empty

Void empty ();

Return Value: The empty operation is not returned;

Example:

Cstring S ("ABC ");

S. Empty ();

Assert (S. getlength () = 0 );

 

23. 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;

Returned value:-1 is returned if the index does not match. The index starts with 0. nstar indicates that the index starts to search 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)

 

24. cstring: findoneof

Int findoneof (lpctstr lpszcharset) const;

Returned value:-1 is returned if the index 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.

 

25. cstring: Format

Void format (lpctstr lpszformat ,...);

Void format (uint nformatid ,...);

Parameter: lpszformat: A format control string

Nformatid string identifier

Example:

Cstring STR;

Str. Format ("% d", 13 );

STR is 13

 

26. cstring: getat

Tchar getat (INT nindex) const;

Return Value: return 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.

 

27. cstring: getbuffer

Lptstr getbuffer (INT nminbuflength );

Return Value: An lptstr pointer pointing to the buffer space of the object (ending with a null character.

Parameter: 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.

Note: This member function returns a pointer to the internal character buffer zone 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 any trailing null characters. 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.

Example:

// 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

 

28. cstring: getlength

Int getlength () const;

Return Value: returns 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 );

 

29. cstring: insert

Int insert (INT nindex, tchar ch );

Int insert (INT nindex, lpctstr pstr );

Return Value: return the modified length. The nindex is the inserted index number of a character (or string ).

Example:

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!

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.