char * string conversion

   // string turn char *

    string str="hello";

    const char*p=str.data();

    char*p=(char*)str.data();

    

    string str="hello";

    const char *p=str.c_str();

    char *p=(char*)str.c_str();

    

    string str="hello";

    char p[50];

    str.copy (p, 5,0); // where 5 represents few characters, 0 represents the position of replication

    * (P + 5) = '\ 0'; // Note terminator was hand! ! !

    

    // char * string transfer

    string str;

    char*p="hello";

    str = p;

    

    // sprintf string concatenation

    string str1="abc";

    string str2="abc";

    char cstr[100];

    sprintf (cstr, "% 2s% .3s.", str1.data (), str2.c_str ()); // character enough to specify several

    sprintf (cstr, "% 02s% 03s", str1.data (), str2.c_str ()); // when the character is not enough to make up 0

    string str=cstr;

 

 

Published 104 original articles · won praise 22 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_41672557/article/details/103285507