The use of MFC Cstring classes

Source: Internet
Author: User
Tags lowercase
CString is located in the header file afx.h.
member functions for CStringthe 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 (LPCTSTR lpch, 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 that does not contain the trailing null character.
Example: csstr= "abcdef Chinese 123456";
printf ("%d", csstr.getlength ()); 16

void Makereverse ();
Reverse the order of strings
Example: csstr= "abcdef Chinese 123456";
Csstr.makereverse ();
cout<<csstr; 654321 text in FEDCBA

void Makeupper ();
Convert lowercase letters to uppercase
Example: csstr= "abcdef Chinese 123456";
Csstr.makeupper ();
cout<<csstr; ABCdef Chinese 123456

void Makelower ();
Convert uppercase letters to lowercase
Example: csstr= "abcdef Chinese 123456";
Csstr.makelower ();
cout<<csstr; ABCdef Chinese 123456

int Compare (LPCTSTR lpsz) const;
Case sensitivity compares two strings, returns 0 when equal, returns 1 when it is less than 1
Example: csstr= "abcdef Chinese 123456";
Csstr2= "abcdef Chinese 123456";
Cout<<csstr.comparenocase (CSSTR2); 0

int comparenocase (LPCTSTR lpsz) const;
Case-insensitive comparison of two strings, equal when returning 0, greater than 1, and less than return-1
Example: csstr= "abcdef Chinese 123456";
Csstr2= "abcdef Chinese 123456";
Cout<<csstr.comparenocase (CSSTR2); -1

int Delete (int nIndex, int ncount = 1)
Delete characters, delete ncount characters starting from subscript nindex
Example: csstr= "ABCDEF";
Csstr.delete (2,3);
cout<<csstr; ABF
The function has no action when the nindex is too large to exceed the memory area of the image.
When nindex is negative, it is deleted from the first character.
Unpredictable results occur when the ncount is too large, resulting in the deletion of characters beyond the memory area where the image is located.
When ncount is negative, the function has no action.

int Insert (int nIndex, TCHAR ch)
int Insert (int nIndex, LPCTSTR pstr)

Inserts a character or string in the position labeled 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, insert at the beginning of the object
Insert at end of object when Nindex is beyond end of object

int Remove (TCHAR ch);
Removes the specified character within the object. Returns the number of removal
Example: csstr= "AABBAACC";
Csstr.remove (' a ');
cout<<csstr; Bbcc

int Replace (TCHAR chold, TCHAR chnew);
int Replace (LPCTSTR lpszold, LPCTSTR lpsznew);

Replace 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 the Chtarget or lpsztargets, and always deletes the first mismatched character.
Example: csstr= "Aaabaacdef";
Csstr.trimleft (' a ');
cout<<csstr; Baacdef
Csstr= "Aaabaacdef";
Csstr.trimleft ("AB");
cout<<csstr; Cdef
Remove spaces when no arguments

void TrimRight ();
void TrimRight (TCHAR chtarget);
void TrimRight (LPCTSTR lpsztargets);

Delete the character from the right, and the deleted character matches the Chtarget or lpsztargets, until the first mismatched character is deleted.
Example: csstr= "ABCDEAAFAAA";
Csstr.trimright (' a ');
cout<<csstr; Abcdeaaf
Csstr= "ABCDEAAFAAA";
Csstr.trimright ("FA");
cout<<csstr; Abcde
Remove spaces when no arguments

void Empty ();
Empty
Example: csstr= "abcdef";
Csstr.empty ();
printf ("%d", csstr.getlength ()); 0

BOOL IsEmpty () const;
Tests whether the object is empty, returns zero when empty, and returns nonzero when not empty
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;

Find the string, Nstart is the location to start looking for. Returns 1 if no match is found, otherwise returns the start of the string
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
Returns 1 when Nstart exceeds the end of the object.
When Nstart is negative, returns-1.

int findoneof (LPCTSTR lpszcharset) const;
Finds the matching position of any character in the Lpszcharset in the CString object. Returns 1 if not found, otherwise returns the starting position of the string
Example: csstr= "abcdef";
Cout<<csstr.findoneof ("Cxy"); 2

CString spanexcluding (LPCTSTR lpszcharset) const;
Returns a substring before the first character in an object that matches any of the Lpszcharset
Example: csstr= "abcdef";
Cout<<csstr.spanexcluding ("CF"); Ab

CString spanincluding (LPCTSTR lpszcharset) const;
Finds a character that does not match any character in Lpszcharse, and returns the string before the first unmatched character
Example: csstr= "abcdef";
Cout<<csstr.spanincluding ("FDCBA"); Abcd

int reversefind (TCHAR ch) const;
Finds the first match from the back forward and returns the subscript when it is found. Returned when not found-1
Example: Csstr= "ABBA";
Cout<<csstr.reversefind (' a '); 3

void Format (LPCTSTR lpszformat, ...);
void Format (UINT nformatid, ...);

Formatted object, same as sprintf function usage for C language
Example: Csstr.format ("%d", 13);
cout<<csstr; 13

TCHAR GetAt (int nIndex) const;
Returns the character labeled Nindex, same as the [] usage of the string
Example: csstr= "abcdef";
Cout<<csstr.getat (2); C
Unpredictable results occur when nindex is negative or exceeds the end of the object.

void SetAt (int nIndex, TCHAR ch);
Reassign a character that is labeled nindex
Example: csstr= "abcdef";
Csstr.setat (2, ' X ');
cout<<csstr; Abxdef
Unpredictable results occur when nindex is negative or exceeds the end of the object.

CString Left (int ncount) const;
Take a string from the left
Example: csstr= "abcdef";
Cout<<csstr.left (3); Abc
When ncount equals 0 o'clock, returns NULL.
When ncount is negative, the return is empty.
When Ncount is greater than the length of the object, the return value is the same as the object.

CString Right (int ncount) const;
Pick a string from the right
Example: csstr= "abcdef";
Cout<<csstr.right (3); Def
When ncount equals 0 o'clock, returns NULL.
When ncount is negative, the return is empty.
When Ncount is greater than the length of the object, the return value is the same as the object.

CString Mid (int nfirst) const;
CString Mid (int nfirst, int ncount) const;

Pick a string from the middle
Example: csstr= "abcdef";
Cout<<csstr.mid (2); Cdef
Csstr= "ABCdef";
Cout<<csstr.mid (2,3); Cde
When Nfirst is 0 and negative, the first character starts fetching.
Returns an empty string when Nfirst equals the end of the object.
Unexpected results occur when the Nfirst exceeds the end of the object.
Returns a string from Nfirst to the end of the object when ncount exceeds the end of the object
When ncount is 0 and negative, an empty string is returned.

LPTSTR GetBuffer (int nminbuflength);
Request 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 you finish using GetBuffer, you must use ReleaseBuffer to update the object's internal data, otherwise unpredictable results can occur.

void ReleaseBuffer (int nnewlength =-1);
After you use GetBuffer, you must use ReleaseBuffer to update the object's internal data
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);
Request 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 have to use ReleaseBuffer after you use 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.