Turn: char*, char[], CString, string conversion

Source: Internet
Author: User

turn: char*, char[], CString, string conversion

(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, usually with "' as the end flag;

(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

Role char* String CString
Find specified value Strchr
Strstr
Strrstr
Strspn
Find Find
The first matching value Fild_first_of Findoneof
Find from the back 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

Role char* String CString
Find specified value (case sensitive) strcmp
strncmp
Strcoll
_strncoll
operator<
Operator>
operator<=
operator>=
operator==
operator!=
Collate

Compare
Find 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

Role char* String CString
Find specified value _strset
_strnset
Replace
Replace_copy
Replace_copy_if
Replace_if
Replace

d) Insert

Role char* String CString
Find specified value Insert Insert


e) Increase

Role char* String CString
Dynamic add Value Strcat Push

Append
Append

Appendchar

AppendFormat


f) Interception

Role char* String CString
Get partial value Use subscript operation Substr Left

Mid

Right

Truncate


g) Remove

Role char* String CString
Remove partial values Remove Remove
To remove a blank value Removeblanks

Note: This is provided for ATL, non-C functions
Remove_if Trim

Trimleft

Trimrig


h) Convert case

Role char* String CString
Convert case _strlwr

_strupr
Makelower

Makeupper


i) and other types of conversions

Role char* String CString
Convert to Digital Atoi

Atod

Atof
Format
Convert to Char* C_str
GetBuffer

Getbuffersetlen

j) Formatting

Role char* String CString
Formatting sprintf Format


k) Get length

Role char* String CString
Get the length Strlen Length GetLength
Get size Size Getalloclength

L) judged to be empty

Role char* String CString
Determines whether the empty Determine if ==null or whether the first character is ' Empty IsEmpty

m) Redefine size

Role char* String CString
Redefine size ReAlloc
New
Resize GetBufferSetLength

N) Releasing Resources

Role char* String CString
Release Free

Delete (delete[])
ReleaseBuffer

Releasebuffersetlength

(v) Safety >

CString > String > char*;

(vi) Flexibility

CString > String >char*;

(vii) Portability

char* = string > CString

(eight) implementation of the conversion            

1.string Turn CString
Cstring.format ("%s", String.c_str ());

2.char Turn CString
Cstring.format ("%s", char*);

3.char to String
string S (char *);

4.string Turn char *
Char *p = STRING.C_STR ();

5.CString Turn string
String s (Cstring.getbuffer ());

6.string-CString
Cstring.format ("%s", String.c_str ());
Using C_str () is really better than data ().
7.CString, String
String s (Cstring.getbuffer ());
GetBuffer () must be releasebuffer (), otherwise the space occupied by the buffer will not be released.
8. There are three functions to convert the contents of a string to a character array and c-string
1.data (), returns a string array without ""
2,C_STR (), returns a string array with ""
3,copy ()

9. Convert characters to integers, you can use Atoi, _atoi64, or ATOL.
10. Convert a number to a CString variable, you can use the CString format function

CString s;
int i = 64;
S.format ("%d", i)
The Format function is very powerful and deserves your research.

11.cstring to char *
Charpoint=strtest. GetBuffer (strtest. GetLength ());

12.CString conversion char[100]

Char a[100];
CString Str ("aaaaaa");
strncpy (A, (LPCTSTR) str,sizeof (a));

Turn: char*, char[], CString, string conversion

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.