Cstring member functions

Source: Internet
Author: User

Http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html

Cstring Constructor


Cstring ();
Example: cstring csstr;

Cstring (const cstring & stringsrc );
Example: cstring csstr ("abcdef Chinese 123456 ");
Cstring csstr2 (csstr );

Cstring (tchar CH, int nrepeat = 1 );
Example: cstring csstr ('A', 5 );
// Csstr = "AAAAA"

Cstring (maid, int nlength );
Example: cstring csstr ("abcdef", 3 );
// Csstr = "ABC"

Cstring (lpcwstr lpsz );
Example: wchar_t s [] = l "abcdef ";
Cstring csstr (s );
// Csstr = l "abcdef"

Cstring (const unsigned char * psz );
Example: const unsigned char s [] = "abcdef ";
Const unsigned char * sp = s;
Cstring csstr (SP );
// Csstr = "abcdef"

Cstring (lpcstr lpsz );
Example: cstring csstr ("abcdef ");
// Csstr = "abcdef"

Int getlength () const;
Returns the length of a string, excluding the trailing null characters.
Example: csstr = "abcdef Chinese 123456 ";
Printf ("% d", csstr. getlength ());
// 16

Void makereverse ();
Reverse the string order
Example: csstr = "abcdef Chinese 123456 ";
Csstr. makereverse ();
Cout <csstr; // CBA in article 654321

Void makeupper ();
Convert lowercase letters to uppercase letters
Example: csstr = "abcdef Chinese 123456 ";
Csstr. makeupper ();
Cout <csstr; // abcdef Chinese 123456

Void makelower ();
Convert uppercase letters to lowercase letters
Example: csstr = "abcdef Chinese 123456 ";
Csstr. makelower ();
Cout <csstr; // abcdef Chinese 123456

Int compare (lpctstr lpsz) const;
Compare two strings in case sensitive. If the value is equal, 0 is returned. If the value is greater than 1, 1 is returned. If the value is smaller than 1,-1 is returned.
Example: csstr = "abcdef Chinese 123456 ";
Csstr2 = "abcdef Chinese 123456 ";
Cout <csstr. comparenocase (csstr2 );
// 0

Int comparenocase (lpctstr lpsz) const;
Compare two strings in case-insensitive mode. If the values are equal, 0 is returned. If the values are greater than 1, 1 is returned. If the values are less than 1,-1 is returned.
Example: csstr = "abcdef Chinese 123456 ";
Csstr2 = "abcdef Chinese 123456 ";
Cout <csstr. comparenocase (csstr2 );
//-1

Int Delete (INT nindex, int ncount = 1)
Delete characters. Delete the ncount characters starting with the subscript nindex.
Example: csstr = "abcdef ";
Csstr. Delete (2, 3 );
Cout <csstr; // abf
// When the nindex is too large and exceeds the memory area of the object, the function does not perform any operations.
// When nindex is negative, it is deleted from the first character.
// When the ncount value is too large and the characters to be deleted exceed the memory area of the object, unexpected results may occur.
// When ncount is a negative number, no operation is performed on the function.

Int insert (INT nindex, tchar ch)
Int insert (INT nindex, lpctstr pstr)

Insert a character or string at the position marked as nindex. Returns the length of the inserted object.
Example: csstr = "ABC ";
Csstr. insert (2, 'x ');
Cout <csstr; // abxc
Csstr = "ABC ";
Csstr. insert (2, "XYZ ");
Cout <csstr; // abxyzc
// When nindex is negative, it is inserted at the beginning of the object
// When nindex exceeds the end of the object, it is inserted at the end of the object.

Int remove (tchar ch );
Removes specified characters from an object. Return the number of removed items.
Example: csstr = "aabbaacc ";
Csstr. Remove ('A ');
Cout <csstr; // bbcc

Int Replace (tchar chold, tchar chnew );
Int Replace (maid, maid );

Replacement string
Example: csstr = "abcdef ";
Csstr. Replace ('A', 'x ');
Cout <csstr; // xbcdef
Csstr = "abcdef ";
Csstr. Replace ("ABC", "XYZ ");
Cout <csstr; // xyzdef

Void trimleft ();
Void trimleft (tchar chtarget );
Void trimleft (lpctstr lpsztargets );

Delete the character from the left. The deleted character matches chtarget or lpsztargets until the first unmatched character is deleted.
Example: csstr = "aaabaacdef ";
Csstr. trimleft ('A ');
Cout <csstr; // baacdef
Csstr = "aaabaacdef ";
Csstr. trimleft ("AB ");
Cout <csstr; // cdef
// Delete spaces when no parameters exist

Void trimright ();
Void trimright (tchar chtarget );
Void trimright (lpctstr lpsztargets );

Delete the character from the right. The deleted character matches chtarget or lpsztargets until the first unmatched character is deleted.
Example: csstr = "abcdeaafaaa ";
Csstr. trimright ('A ');
Cout <csstr; // abcdeaaf
Csstr = "abcdeaafaaa ";
Csstr. trimright ("Fa ");
Cout <csstr; // ABCDE
// Delete spaces when no parameters exist

Void empty ();
Clear
Example: csstr = "abcdef ";
Csstr. Empty ();
Printf ("% d", csstr. getlength (); // 0

Bool isempty () const;
Whether the test object is null. If it is null, zero is returned. If it is not null, non-zero is returned.
Example: csstr = "ABC ";
Cout <csstr. isempty ();
// 0;
Csstr. Empty ();
Cout <csstr. isempty ();
// 1;

Int find (tchar ch) const;
Int find (lpctstr lpszsub) const;
Int find (tchar CH, int nstart) const;
Int find (lpctstr pstr, int nstart) const;

Query string. nstart is the start position. -1 is returned if no matching is found; otherwise, the start position of the returned string is returned.
Example: csstr = "abcdef ";
Cout <csstr. Find ('B'); // 1
Cout <csstr. Find ("de"); // 3
Cout <csstr. Find ('B', 3); //-1
Cout <csstr. Find ('B', 0); // 1
Cout <csstr. Find ("de", 4); //-1
Cout <csstr. Find ("de", 0); // 3
// If nstart exceeds the end of the object,-1 is returned.
// If nstart is negative,-1 is returned.

Int findoneof (lpctstr lpszcharset) const;
Find the matching position of any character in the lpszcharset in the cstring object. -1 is returned if not found; otherwise, the start position of the returned string is returned.
Example: csstr = "abcdef ";
Cout <csstr. findoneof ("cxy"); // 2

Cstring spanexcluding (lpctstr lpszcharset) const;
Returns the substring before the first character that matches any lpszcharset in the object.
Example: csstr = "abcdef ";
Cout <csstr. spanexcluding ("CF"); // AB

Cstring spanincluding (lpctstr lpszcharset) const;
Find any character that does not match any character in lpszcharse from the object, and return the string before the first character that does not match
Example: csstr = "abcdef ";
Cout <csstr. spanincluding ("fdcba"); // ABCD

Int reversefind (tchar ch) const;
Search for the first match from the back to the front. The subscript is returned when the match is found. -1 is returned when no result is found.
Example: csstr = "Abba ";
Cout <csstr. reversefind ('A'); // 3

Void format (lpctstr lpszformat ,...);
Void format (uint nformatid ,...);

Format the object. It is used in the same way as the sprintf function in C language.
Example: csstr. Format ("% d", 13 );
Cout <csstr;
// 13

Tchar getat (INT nindex) const;
Returns the nindex character, which is used in the same way as the string [].
Example: csstr = "abcdef ";
Cout <csstr. getat (2 );
// C
// When nindex is a negative number or exceeds the end of the object, unexpected results will occur.

Void setat (INT nindex, tchar ch );
Assign a value to the character whose subscript is nindex.
Example: csstr = "abcdef ";
Csstr. setat (2, 'x ');
Cout <csstr; // abxdef
// When nindex is a negative number or exceeds the end of the object, unexpected results will occur.

Cstring left (INT ncount) const;
Returns the string from the left.
Example: csstr = "abcdef ";
Cout <csstr. Left (3); // ABC
// If ncount is equal to 0, null is returned.
// If ncount is negative, null is returned.
// When ncount is greater than the object length, the returned value is the same as that of the object.

Cstring right (INT ncount) const;
Extract string from right
Example: csstr = "abcdef ";
Cout <csstr. Right (3 );
// Def
// If ncount is equal to 0, null is returned.
// If ncount is negative, null is returned.
// When ncount is greater than the object length, the returned value is the same as that of the object.

Cstring mid (INT nfirst) const;
Cstring mid (INT nfirst, int ncount) const;

Returns the string starting from the center.
Example: csstr = "abcdef ";
Cout <csstr. mid (2 );
// Cdef
Csstr = "abcdef ";
Cout <csstr. mid (2, 3 );
// CDE
// When nfirst is 0 and negative, it starts from the first character.
// If nfirst is equal to the end of the object, an empty string is returned.
// When nfirst exceeds the end of the object, unexpected results will occur.
// When ncount exceeds the end of the object, the string from nfirst to the end of the object is returned.
// When ncount is 0 and negative, an empty string is returned.

Lptstr getbuffer (INT nminbuflength );
Apply for a new space and return a pointer
Example: csstr = "ABCDE ";
Lptstr pstr = csstr. getbuffer (10 );
Strcpy (pstr, "12345 ");
Csstr. releasebuffer ();
Pstr = NULL;
Cout <csstr // 12345
// After using getbuffer, you must use releasebuffer to update internal data of the object. Otherwise, unexpected results may occur.

Void releasebuffer (INT nnewlength =-1 );
After getbuffer is used, releasebuffer must be used to update internal data of the object.
Example: csstr = "ABC ";
Lptstr pstr = csstr. getbuffer (10 );
Strcpy (pstr, "12345 ");
Cout <csstr. getlength ();
// 3 (incorrect usage)
Csstr. releasebuffer ();
Cout <csstr. getlength ();
// 5 (correct)
Pstr = NULL;
// Any method of the cstring object should be called after releasebuffer

Lptstr getbuffersetlength (INT nnewlength );
Apply for a new space and return a pointer
Example: csstr = "ABC ";
Csstr. getbuffersetlength (20 );
Cout <csstr; // ABC
Count <csstr. getlength (); // 20;
Csstr. releasebuffer ();
Count <csstr. getlength ();
// 3;
// You do not need to use releasebuffer after using getbuffersetlength.

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.