1.常量字符串 "abc" 默认为const char []
2.string 初始化方式 =/()
3.索引string中的字符用 []/.at()
4.string.data() //返回一个非null终止的c字符数组
string.c_str()//返回一个以null终止的c字符串
string.copy(char *s, int n, int pos = 0) //把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
5.字符串的拆分与合并
string input("hello,this is a test");
istringstream is(input);
string s1,s2,s3,s4;
is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os<
cout<
6.数字与字符串互转
数字转字符串
//方法一
int value = 20;
string result;
stringstream buffer;
buffer << std::hex << value;
buffer >> result;
//方法二
int value = 33;
ostringstream os;
string str;
os << value << endl;
str = os.str();
字符串转数字
//方法一
string value = "123";
int result;
stringstream buffer;
buffer << std::dec << value;
buffer >> result;
//方法二
string value = "123";
int result;
stringstream ss(value);
ss >> result;
7.string/char *相关类型互转
char * s = "hello world";
const char * cs = "world";
string str;
s = const_cast(cs); // const char* -> char*
cs = s; // char* -> const char*
str = s; // char* -> string
s = const_cast(str.c_str());// string -> char*
string tmp = cs;//const char * -> string
cs = str.c_str();//string -> const char *
阅读(1484) | 评论(0) | 转发(0) |