Conversion between character array and string in C++

 

The character array is converted into string type
char ch [] = "ABCDEFG";
string str(ch);//It can also be string str = ch;
or
char ch [] = "ABCDEFG";
string str;
str = ch;//In the original On the basis of adding, you can use str += ch; to

convert the string type to a character array
char buf[10];
string str("ABCDEFG");
length = str.copy(buf, 9);
buf[length] ='\ 0';
or
char buf[10];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);

Guess you like

Origin blog.csdn.net/nyist_yangguang/article/details/114544118