CString and string

Source: Internet
Author: User

CString and string
(i) overview

String and CString are string template classes, string classes defined by the Standard template class (STL), are included in the C + + standard;

CString (typedef CStringT > CString) is the most commonly used string class in Visual C + + and inherits from the CSimpleStringT class, mainly used in MFC and ATL programming, with char (applied to ANSI) for main data types , wchar_t (Unicode), TCHAR (ANSI and Unicode are available);

Char* is the most commonly used string pointer in C programming, and generally takes '/' as the end sign;

(ii) Construction

The string is convenient and can be constructed from almost all strings, including CString and char*;

CString second, can be constructed from some basic string variables, including char* and so on;

Char* does not have a constructor, only can be assigned value;

Example:

char* psz = "Joise";

CString CStr (PSZ);

String str (CSTR);

(c) Operator overloading

A) operator=

String is the most convenient and can be used to assign values to almost all strings, including CString and char*;

CString second, can directly use some basic string assignment, including char* and so on;

Char* can only be assigned by pointers, and is extremely dangerous operation, it is recommended to use strcpy or memcpy, and char* at the time of declaration, if the initial value is not assigned to the proposed first set to NULL, to avoid wild pointers, so you are crazy;

Example:

char *psz = NULL;

Psz = new CHAR[10]; Of course, the above is directly written as char *psz = new CHAR[10];

memset (psz, 0, 10);

strcpy (Psz, "joise");

CString CStr;

CStr = Psz;

String str;

str = PSZ;

str = CStr;

delete []psz;

b) operator+

String is similar to CString, can be directly with the char* to add, but can not use the + operator, that is, string str = str + CSTR is illegal, must be converted to char*;

Char* does not have a + operation, can only use strcat to connect two pointers together;

Example:

char* psz = "Joise";

CString CStr = psz;

CStr = CStr + psz;

string str = Psz;

str = str + str + psz;

strcat (Psz, psz);

strcat (Psz, CStr);//Legal

strcat (Psz, str);//Illegal, this shows that CString can be automatically converted to a const char*, and string does not

c) operator + =

String is the most powerful and can be used with all string variables + =, including CString and char*;

CString second, can be compared with some basic string variables + =, including char*, etc.;

Char* does not have the + = operator and can only use strcat to connect two pointers together;

d) operator[]

CString is best when an assertion exception is thrown when crossing the bounds;

String and char* subscript out-of-bounds results not defined;

Example:

char* psz = "Joise";

CString CStr = psz;

cout << cstr[8];

string str = Psz;

cout << str[8];

cout << psz[8];

e) operator==, operator!=, operator>, operator<, operator>=, perator<=

CString and string can not be compared, but can be compared with char*, and the comparison is the value, not the address;

cout << (Psz = = CStr);

cout << (psz = = str);

cout << (str = = Psz);

cout << (CStr = = psz);//The above code return is 1

(iv) Common algorithms

A) Find

function char* String CString
Finds the specified value STRCHR
Strstr
Strrstr
Strspn Find Find
The first matching value fild_first_of findoneof from the back to find Reservefind specify the matching method find_if
Note: In find_if, the values in the range are put into the matching function until True

b) Comparison


function char* string CString to find the specified value (case-sensitive) strcmp
strncmp
Strcoll
_strncoll operator<
Operator>
operator<=
operator>=
operator==
operator!= Collate

Compare find the specified value (case insensitive) _stricmp
_strnicmp
_stricoll
_strnicoll Collatenocase

Comparenocas
Note: The return value if <0 is lower than the value that follows, and vice versa

c) Replace

function char* string CString to find the specified value _strset
_strnset
Replace
Replace_copy
Replace_copy_if
Replace_if Replace
d) Insert

function char* string CString Find the specified value insert insert
e) Add action char* string CString Dynamic add value strcat push

Append Append

Appendchar

AppendFormat

f) Interception

function char* string CString get partial value with subscript operation Substr left

Mid

Right

Truncate

g) Remove

function char* string CString Remove the partial value remove remove removes the blank value removeblanks

Note: This is provided for ATL, non-C function remove_if Trim

Trimleft

Trimrig

h) Convert case

function char* string CString conversion case _STRLWR

_STRUPR Makelower

Makeupper

i) and other types of conversions

function char* string CString into a digital atoi

Atod

Convert atof Format to char* c_str
GetBuffer

Getbuffersetlen
j) Formatting

Action char* string CString formatting sprintf format


k) Get length

function char* String CString
Get the length of strlen length getlength get the size getalloclength
L) judged to be empty

function char* string CString to determine whether the null is ==null or whether the first character is ' \ s ' Empty IsEmpty
m) Redefine size

function char* string CString redefine size realloc
New Resize GetBufferSetLength
N) Releasing Resources

function char* string CString free

Delete (delete[]) ReleaseBuffer

Releasebuffersetlength

(v) Safety >

CString > String > char*;

(vi) Flexibility

CString > String >char*;

(vii) Portability

char* = string > CString

CString and string

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.