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);