Convert: char *, char [], cstring, the relationship between strings and some common functions

Source: Internet
Author: User
Original article address:Convert: char *,
Char [] , Cstring, String Conversion Author:Gisldq

(1) Overview

String and cstring are both string template classes. string is a string class defined by the standard template class (STL) and has been incorporated into the C ++ standard;

Cstring (typedef cstringt> cstring) is the most common string class in Visual C ++. It inherits from the csimplestringt class and is mainly used in MFC and ATL programming, the main data types include char (used in ANSI), wchar_t (UNICODE), and tchar (both ANSI and Unicode );

Char * is the most commonly used string pointer in C programming. It generally ends;

(2) Construction

String is convenient and can be constructed from almost all strings, including cstring and char *;

Followed by cstring, which can be constructed from some basic string variables, including char;

Char * has no constructor and can only be assigned a value;


Char * psz = "joise ";

Cstring CSTR (psz );

String STR (CSTR );

(3) Operator Overloading

A) operator =

String is the most convenient, and almost all strings can be assigned directly, including cstring and char *;

Followed by cstring. You can directly assign values using some basic strings, including char;

Char * can only be assigned values by pointers, which is extremely dangerous. We recommend that you use strcpy or memcpy. In addition, if the initial value is not assigned to char *, we recommend that you set it to null first, to avoid wild pointers and drive you crazy;


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;


Delete [] psz;

B) operator +

String is similar to cstring. It can be added directly to char *, but the + operator cannot be used with each other. That is, string STR = STR + CSTR is invalid and must be converted to char *;

Char * has no + operation. Only two pointers can be connected using strcat;


Char * psz = "joise ";

Cstring CSTR = psz;

CSTR = CSTR + psz;

String STR = psz;

STR = STR + psz;

Strcat (psz, psz );

Strcat (psz, CSTR); // valid

Strcat (psz, STR); // invalid. Therefore, cstring can be automatically converted to const char *, but string cannot.

C) operator ++ =

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

Followed by cstring, which can be + = with some basic string variables, including char;

Char * has no + = Operator and can only use strcat to connect two pointers;

D) operator []

The cstring is the best. When a cross-border request is made, an asserted exception is thrown;

The results of the out-of-bounds string and char * subscripts are undefined;


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 cannot be compared, but both can be compared with char * and compared with the value rather than the address;

Cout <(psz = CSTR );

Cout <(psz = Str );

Cout <(STR = psz );

Cout <(CSTR = psz); // The code above returns 1

(4) common algorithms

A) Search

Function Char * String Cstring
Search for a specified value Strchr
Find Find
The first matched Value   Fild_first_of Findoneof
Start searching later     Reservefind
Matching Method   Find_if  

Note: In find_if, values in the range are substituted into the matching function one by one until true is returned.

B) Comparison

Function Char * String Cstring
Search for a specified value (case sensitive) Strcmp
_ Strncoll
Operator <
Operator <=
Operator> =
Operator =
Operator! =


Search for the specified value (Case Insensitive) _ Stricmp
_ Strnicmp
_ Stricoll
_ Strnicoll


Note: If the return value is <0, the previous value is smaller than the subsequent value, and vice versa.

C) replace

Function Char * String Cstring
Search for a specified value _ Strset
_ Strnset

D) insert

Function Char * String Cstring
Search for a specified value   Insert Insert

E) add

Function Char * String Cstring
Dynamic value-added Strcat Push





F) intercept

Function Char * String Cstring
Get part score Subscripts Substr Left




G) Remove

Function Char * String Cstring
Remove partial values   Remove Remove
Remove Blank Value Removeblanks

Note: This is provided by ATL. It is not a C function.

Remove_if Trim



H) case-insensitive Conversion

Function Char * String Cstring
Case sensitivity _ Strlwr

_ Strupr



I) conversion from other types

Function Char * String Cstring
Convert to number Atoi



Convert to char *   C_str Getbuffer


J) Format

Function Char * String Cstring
Format Sprintf   Format

K) Get the length

Function Char * String Cstring
Get Length Strlen Length Getlength
Get size   Size Getalloclength

L) null judgment

Function Char * String Cstring
Judge whether it is empty Determine whether = NULL or whether the first character is'' Empty Isempty

M) redefinition size

Function Char * String Cstring
Redefinition size Realloc
Resize Getbuffersetlength

N) release resources

Function Char * String Cstring
Release Free

Delete (Delete [])



(5) Security>

Cstring> string> char *;

(6) Flexibility

Cstring> string> char *;

(7) portability

Char * = string> cstring

(8) Implementation of conversion

1. Convert string to cstring
Cstring. Format ("% s", String. c_str ());

2. Convert Char to cstring
Cstring. Format ("% s", char *);

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

4. Convert string to char *
Char * P = string. c_str ();

5. Convert cstring to string
String S (cstring. getbuffer ());

6. String-> cstring
Cstring. Format ("% s", String. c_str ());
C_str () is indeed better than data.
7. cstring-> string
String S (cstring. getbuffer ());
Releasebuffer () is required after getbuffer (). Otherwise, no space occupied by the buffer is released.
8. There are three functions that can convert the content of a string to a character array and a C-string
1. Data (), returns a string array ""
2, c_str (), returns a string array ""
3, copy ()

9. Convert the character to an integer. You can use atoi, _ atoi64, or atol.
10. convert numbers to cstring variables. You can use the format function of cstring.

Cstring S;
Int I = 64;
S. Format ("% d", I)
The format function is very powerful and worth 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 ());

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: 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.