"Reprint" C++--cstring usage Daquan

Source: Internet
Author: User

Introduction to common methods of CString
Webmaster Source: None


Cstring::compare
int Compare (LPCTSTR lpsz) const;
Returns a value string like 0
Less than lpsz return-1
Greater than Lpsz returns 1
Distinguish between size characters
CString S1 ("abc");
CString S2 ("abd");
ASSERT (s1.compare (s2) = =-1);
ASSERT (S1.compare ("abe") = =-1);

Cstring::comparenocase
int CompareNoCase (LPCTSTR lpsz) const;
Returns a value string like 0
Less than lpsz return-1
Greater than Lpsz returns 1
The size character is not distinguished

Cstring::collate
int Collate (LPCTSTR lpsz) const;
With Cstring::compare

Cstring::collatenocase
int collatenocase (LPCTSTR lpsz) const;
With Cstring::comparenocase

Cstring::cstring
CString ();
CString (const cstring& STRINGSRC);
CString (TCHAR ch, int nrepeat = 1);
CString (LPCTSTR lpch, int nlength);
CString (const unsigned char* psz);
CString (LPCWSTR lpsz);
CString (LPCSTR lpsz);
Examples are the easiest to explain the problem
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::D elete
int Delete (int nIndex, int ncount = 1);
The return value is the length of the string before being deleted
Nindex is the first character to be deleted, and ncount is the one that deletes several characters at a time. According to the results of my experiment: when ncount> to delete the maximum length of a string (GetCount ()-NIndex), an error occurs when the ncount is too large and there are not enough characters to delete, this function does not execute.
Example
CString STR1,STR2,STR3;
Char A;
str1 = "Nihao";
str2 = "Nihao";
int x;
int i= (str1 = = str2);
Str1. Delete (2,3);
If Ncount (3) > GetCount () –nindex (5-2) will execute the error

Cstring::empty
Void Empty ();
No return value emptied 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;
Returns 1 if the return value does not match; Index starts at 0
NStar to start the search with the character Nstart the index value.
Is the string containing the Nstart character after the index
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;
Returns 1 if the return value does not match; Index starts at 0
Note: Returns the zero-based index value of the first in this string that also includes a character in Lpszcharset
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 identifiers
Example
CString str;
Str.format ("%d", 13);
At this point str is 13

Cstring::getat
TCHAR GetAt (int nIndex) const;
Return the character labeled nindex, you can interpret the string as an array, getat similar to []. Note the scope of nindex, if not appropriate, there will be debugging errors.

Cstring::getbuffer
LPTSTR GetBuffer (int nminbuflength);
return value
A lptstr pointer to the character buffer (at the end of the null character) that points to the object.
Parameters
Nminbuflength
The minimum capacity of the character buffer as a number of characters. This value does not include space for a trailing null character.
Description
This member function returns a pointer to the internal character buffer of the CString object. The returned LPTSTR is not const, so you can allow direct modification of the contents of the CString. If you use a pointer returned by GetBuffer to change the contents of a string, you must call the ReleaseBuffer function before using another CString member function.
After calling ReleaseBuffer, the address returned by GetBuffer may be invalid because other CString operations may cause the CString buffer to be reassigned. If you do not change the length of this CString, the buffer will not be reassigned. When this CString object is destroyed, its buffer memory is automatically freed.
Note that if you know the length of the string yourself, you should not add the trailing null character. However, when you use ReleaseBuffer to release the buffer, you must specify the last string length. If you add a null character at the end, you should pass 1 to the length parameter of the ReleaseBuffer, and ReleaseBuffer will execute strlen on the buffer to determine its length.
The following example shows how to use the Cstring::getbuffer.
Cstring::getbuffer Example
CString s ("ABCD");
#ifdef _DEBUG
AfxDump << "CString S" << s << "\ n";
#endif
LPTSTR p = s.getbuffer (10);
strcpy (P, "Hello"); Direct access to the CString object.
S.releasebuffer ();
#ifdef _DEBUG
AfxDump << "CString S" << s << "\ n";
#endif

Cstring::getlength
int getlength () const;
return value
Returns the count of bytes in a string.
Description
This member function is used to get the count of bytes in this CString object. This count does not include the trailing null character.
For multibyte character sets (MBCS), GetLength is counted by each 8-bit character, that is, the start and end bytes in a multibyte character are counted as two bytes.
Example
The following example shows how to use the 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 an example of the index number after the insertion 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 value other than 0 is returned, otherwise 0 is returned.
Description
This member function is used to test whether a CString object is empty.
Example
The following example shows how to use the 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
Returns a value other than 0 if the load resource succeeds; otherwise 0.
NID a Windows String resource ID.
Description This member function is used to read a Windows String resource identified by NID and put into an existing CString object.
Example
The following example shows how to use the cstring::loadstring.
cstring::loadstring Example
#define IDS_FILENOTFOUND 1
CString s;
if (! s.loadstring (Ids_filenotfound))

Cstring::makelower
void Makelower ();
Change the lowercase of a character

Cstring::makereverse
void Makereverse ();
Character inversion

Cstring::makeupper
void Makeupper ();
Change the capitalization of a character
Cstring::mid
CString Mid (int nfirst) const;
CString Mid (int nfirst, int ncount) const;
Ncount represents the number of characters to extract, and Nfirst represents the starting index position to extract
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 as a number of characters, and does not calculate the trailing null character. If the word
String is terminated with a null character, the default value of the parameter, 1, sets the size of the CString to
The current length of the string.
Description
Use ReleaseBuffer to end the use of buffers allocated by GetBuffer. If you know the slow
The string in the flushing area ends with a null character, you can omit the Nnewlength parameter. If the character
String is not terminated with a null character, you can use Nnewlength to specify the length of the strings. In the call
The address returned by GetBuffer is not valid after ReleaseBuffer or other CString operations.
Example
The following example shows how to use the Cstring::releasebuffer.
Cstring::releasebuffer Example
CString s;
s = "abc";
LPTSTR p = s.getbuffer (1024);
strcpy (P, "abc"); Use this buffer directly
ASSERT (s.getlength () = = 3); String length = 3
S.releasebuffer (); Free the extra memory, now p is invalid.
ASSERT (s.getlength () = = 3); Length is still 3

Cstring::remove
int cstring::remove (TCHAR ch);
return value
Returns the number of characters removed from the string. Returns zero if the string does not change.
Parameters
Ch
The character to remove from a string.
Description
This member function is used to remove the ch instance from the string. Comparisons with this character are case-sensitive
Of
Example
Remove the lowercase 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 (LPCTSTR lpszold, LPCTSTR lpsznew);
return value
Returns the number of characters that have been replaced. Returns zero if the string does not change.
Parameters
Chold
The character to be replaced by the chnew.
Chnew
The character to use to replace the chold.
Lpszold
A pointer to a string that contains the characters to be replaced by Lpsznew.
Lpsznew
A pointer to a string containing the character to be used to replace the lpszold.
Description
This member function replaces another character with one character. The first prototype of a function is used in a string chnew
Field Replacement Chold. The second prototype of the function replaces lpszold specified with a string specified by Lpsznew
Substring of the string.
After substitution, the string is likely to grow or shorten; that's because the lengths of Lpsznew and Lpszold
Do not need to be equal. Both versions form a case-sensitive match.

http://blog.163.com/ccd_ok/blog/static/310238892007510113223798/

"Reprint" C++--cstring usage Daquan

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.