Differences between cstring and string and their conversion

Source: Internet
Author: User

When using MFC for programming, the string obtained by using getwindowtext in the dialog box is of the cstring type, and the cstring belongs to the MFC class. Some Standard C/C ++ library functions cannot directly operate on the cstring type, so we often encounter the situation of converting the cstring type to char * and other data types. Here is a summary!

First of all, it should be clear that there is no string type in Standard C, and string is a class for Standard C ++ to expand string operations. However, we know that the header file string. H is included in standard C. It should be distinguished here. This string is not another string.The string. h header file defines some of the frequently used string operation functions, such as strcpy, strcat, and strcmp. However, the operation objects of these functions are strings pointed to by char. The string operation object of C ++ is a string type string. This class reinstalls some operators and adds some string operation member functions to make the operation string more convenient. Sometimes we need to use the string and char * strings together, so the conversion of these two types will also be involved.

1. cstring
And String Conversion

 String STR = "  Ksarea  " ;
Cstring CSTR (Str. c_str ()); // Or cstring CSTR (Str. Data (); during initialization
CSTR = Str. c_str (); // Or CSTR = Str. Data ();
STR = CSTR. getbuffer ( 0 ); // Cstring-> string
CSTR. Format ( " % S " , Str. c_str ()); // String-> cstring
CSTR. Format ( " % S " , Str. Data ()); // String-> cstring
STR = lpcstr (CSTR ); // Cstring-> string

/*The difference between c_str () and data () is that the former returns a string with '\ 0', and the latter returns a string without' \ 0 '.*/

2. cstringAnd int Conversion

IntI =123;
Cstring STR;
Str. Format ("% D", I );//INT-> other basic types of cstring conversion are similar.
I = atoi (STR );//Cstring-> int and (atof, ATOL)

3. char *And cstring Conversion

Cstring CSTR ="Ksarea";
Char* Ptemp = CSTR. getbuffer (0);
Char* STR;
Strcpy (STR, ptemp );//Cstring-> char *
CSTR. releasebuffer (-1);
Char* STR ="Lovesha";
Cstring CSTR = STR;//Char *-> cstring type cannot be directly assigned to cstring

The conversion between int, float, string, and char * can be forced conversion, or the standard library function.

There are many conversion methods for cstring and other types, but they all share the same path. In the same direction, the type is first converted to char *, because char * is a bridge between different types.
To obtain the char * type, it is very easy to convert it to another type.
C ++ standard is used to discard char * stringsProgramThe string class in the library, because it is compared with the former, does not have to worry about whether the memory is sufficient, string length, and so on, and as a class, the integrated operation functions are sufficient to meet our needs in most cases (or even 100%. We can use = to assign values, = to compare, + to concatenate and so on. We can think of it as the basic data type of C ++.

[Thank you for your reference]
Http://blog.csdn.net/bitxinhai/archive/2008/04/14/2292014.aspx

[Other]
1. header file of string: <iostream>, <string>, using namespace STD;
2. cstring header file: <afx. h>

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.