天天看点

string转换char*   //string转char*    //char*转string    //sprintf拼接字符串

   //string转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);//这里5代表几个字符,0代表复制的位置

    *(p+5)='\0';//注意手动加结束符!!!

    //char*转string

    string str;

    char*p="hello";

    str=p;

    //sprintf拼接字符串

    string str1="abc";

    string str2="abc";

    char cstr[100];

    sprintf(cstr,"%.2s%.3s",str1.data(),str2.c_str());//字符足够,指定要几位

    sprintf(cstr,"%02s%03s",str1.data(),str2.c_str());//字符不够时,补0

    string str=cstr;

继续阅读