- string assign函数可以用来赋值
- 将字符串转换成字符数组
1: basic_string &assign( const basic_string &str );
2: //用str为字符串赋值
3: basic_string &assign( const char *str );
4: //用字符数组进行赋值
5: basic_string &assign( const char *str, size_type num );
6: //用str的开始num个字符为字符串赋值,
7: basic_string &assign( const basic_string &str, size_type index, size_type len );
8: //用str的子串为字符串赋值,子串以index索引开始,长度为len
9: basic_string &assign( size_type num, char ch );
10: // 用num个字符ch为字符串赋值
11:
string s; char ch[100]; strcpy(ch, "hello,world"); s.assign(ch); //将字符数组转换成字符串 cout << s << endl;
string s; char ch[100]; s = "hello,world"; /*strcpy(ch, s.c_str()); //第一种方法 cout << ch;*/ strncpy(ch, s.c_str(), s.length() + 1);// 长度为s.length()+1,因为有结束符 cout << ch;
char *strncpy( char *to, const char *from, size_t count ); //字符串from 中至多count个字符复制到字符串to中。如果字符串from 的长度小于count,其余部分用'\0'填补。返回处理完成的字符串。 const char *c_str(); //c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.
来源:https://www.cnblogs.com/cyj1258/p/12183081.html