字符串头文件:
#include<string>
1 构造与操作
1.1 字符串的构造
string的构造函数的形式:
string str:生成空字符串
string s(str):生成字符串为str的复制品
string s(str, strbegin, strlen):将字符串str中从下标strbegin开始、长度为strlen的部分作为字符串初值
string s(cstr, char_len):以C_string类型cstr的前char_len个字符串作为字符串s的初值
string s(num, c):生成num个c字符的字符串
string s(str, stridx):将字符串str中从下标stridx开始到字符串结束的位置作为字符串初值
eg:
string str0;
string str1 = "123456789" //123456789
string str2("123456789"); //123456789
string str3("12345", 0, 3); //123
string str4("012345", 5); //01234
string str5(5, '1'); //11111
string str6(str2, 2); //3456789
1.2 字符串的操作
int main() {
string str = "hello world";
for (int i = 0; i < str.size(); i++) {
cout << str[i];
}
cout << endl << "the 7th element of str is: " << str[6] << endl;
string str1 = "to be question";
string str2 = "that is a ";
string str3 = "or not world";
string str4;
str4.insert(0, str1); //to be question
str4.insert(6, str3, 0, 7); //to be or not question
str4.insert(13, "to be "); //to be or not to be question
str4.insert(19, str2); //to be or not to be that is a question
cout << "str4: " << str4 << endl;
erase(int start, int len);
//start为要删除字符的起始位置(从0数起),len为要删除字符的个数。
str4.erase(19); //to be or not to be
str4.erase(0,9) //not to be
cout << "str4: " << str4 << endl;
str4.clear();
cout << "str4: " << str4 << endl;
return 0;
}
运行结果:
hello world
the 7th element of str is: w
str4: to be or not to be that is a question
str4: to be or not to be
str4:
1.3 字符串的拼接
- 字符串进行加减时,不能有表达式,可以先把一个表达式赋值给变量,然后再用变量进行加减
int main() {
string str1 = "to be";
string str2 = "not to be";
string str3 = " that is a question";
string str = str1 + ", "; //"to be , "
str = str + str2 + ';'; //"to be , not to be;"
str += str3; //"to be , not to be; that is a question"
cout << str << endl;
str1 = "ab";
str2 = "abc";
str3 = "bcc";
//按字母序列进行比较
cout << (str1 <= str2) << endl; //true
cout << (str2 != str3) << endl; //true
cout << (str3 < str1) << endl; //false
return 0;
}
1.4 字符串的函数
1.4.1 size()
功能:返回字符串的长度
string str = "hello world";
int n= str.size();
printf("%d\n", n);
1.4.2 substr()
功能:从指定位置开始复制指定长度的子字符串
basic_string substr(size_type _Off = 0,size_type _Count = npos) const;
int main() {
string str = "abc.123xyz";
cout << str << endl;
cout << str.substr(4, 7) << endl;
cout << str.substr(str.find('.') + 1) << endl;
cout << str.substr(0,str.find('.')) << endl;
cout << str.substr() << endl;
return 0;
}
执行结果:
abc.123xyz
123xyz
123xyz
abc
abc.123xyz
1.4.3 to_string()
功能:将数字常量转换为字符串
参数:value,整数类型
返回值:转换好的字符串
string to_string(int value)
1.4.4 find()
- 功能:查找相应的字符或者字符串,查找成功则返回相应下标,不成功则返回-1
2 流输入输出(stringstream)
2.1 概述
<sstream> 主要用来进行数据类型转换,由于 <sstream> 使用 string 对象来代替字符数组(snprintf方式),就避免缓冲区溢出的危险;而且因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接。
2.2 示例
2.2.1 数据类型转换
//省略头文件以及名字空间
int main()
{
stringstream sstream;
string strResult;
int nValue = 1000;
// 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult;
cout << "[cout]strResult is: " << strResult << endl;
printf("[printf]strResult is: %s\n", strResult.c_str());
return 0;
}
该代码可将数值转换为字符串,执行结果为:
[cout]strResult is: 1000
[printf]strResult is: 1000
2.2.2 字符串拼接
//省略头文件以及名字空间
int main()
{
stringstream sstream;
// 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
cout << "strResult is: " << sstream.str() << endl;
// 清空 sstream
sstream.str("");
sstream << "third string";
cout << "After clear, strResult is: " << sstream.str() << endl;
return 0;
}
该代码执行结果如下:
strResult is: first string, second string
After clear, strResult is: third string
- 通过str()方法,可以将 stringstream 类型转换为 string 类型
- 可以将多个字符串放入stringstream中,实现字符串拼接
- 如果想清空 stringstream,需使用 sstream.str(" ")
2.2.3 字符串输入输出
通过 stringstream 可以逐字输出字符串(以 ‘ ’ 为分隔)
//省略头文件以及名字空间
int main() {
string s = "1 23 # 4";
stringstream ss;
ss << s;
while(ss >> s){
cout << s << endl;
}
return 0;
}
运行结果如下:
1
23
#
4
2.2.4 清空
清空 stringstream 有两种方法:clear() 方法以及 str(" ") 方法,这两种方法有不同的使用场景。
//省略头文件以及名字空间
int main()
{
stringstream sstream;
int first, second;
// 插入字符串
sstream << "456";
// 转换为int类型
sstream >> first;
cout << first << endl;
// 在进行多次类型转换前,必须先运行clear()
sstream.clear();
// 插入bool值
sstream << true;
// 转换为int类型
sstream >> second;
cout << second << endl;
return 0;
}
运行结果如下:
456
1
在进行多次数据类型转换时,必须使用 clear() 方法清空 stringstream,不使用clear()或使用str(" ")方法,都不能得到数据类型转换的正确结果