1. 将字符数组char[]转化为字符串string
char ch [] = “ABCDEFG”;
string str(ch);//也可string str = ch;
//或者
char ch [] = “ABCDEFG”;
string str;
str = ch;//在原有基础上添加可以用str += ch;
2. 将字符串string转化为字符数组char[]
char buf[10];
string str(“ABCDEFG”);
length = str.copy(buf, 9);
buf[length] = ‘\0’;
//或者(推荐)
char buf[10];
string str(“ABCDEFG”);
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);
原文地址:https://www.cnblogs.com/blink-cz/p/12242304.html
时间: 2024-12-15 21:23:06