string, char *, char [] conversion

1、string->char*

(1)data

string s = "goodbye";
const char* p=str.data();

(2) c_str ()

string s = "goodbye";
const char* p=str.c_str();

(3)copy

String STR = " HMMM " ;
 char P [ 50 ]; 
str.copy (P, 5 , 0 ); // where 5 represents a replication of several characters, 0 represents the position of replication, 
* (P + 5 ) = '\ 0 ' ; // Note that manually adding endings! ! !

2、char* -> string

Direct assignment.

String S;
 char * P = " Hello " ; // direct assignment 
s = p;

3、string->char[]

First draw length, and then assign the characters one by one

string s1="hello";
char s2[6];
int i=0;
for(;i<s1.length();i++) s2[i]=s1[i];
s2[i] = '\0';

4、char->string

Direct assignment

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11264504.html