string
1、头文件
#include<string>
2、初始化
string s{"hello world"};
string s1{6,'z'};//zzzzzz
string s2{s};
string s3{"hello world",5};//hello
string s4{"hello world",7,5};//world,开始位置,字符个数
3、添加
//添加子串
string s5=s+s;//hello worldhello world
string s6=s+"hello world";//hello worldhello world
//不能 s6="hello"+"hello",两个字面意义上的字符串不能相加
4、删除
s.erase(0,6);//world,开始索引,删除字符个数
s.clear();//清除
5、截取
string s7=s.substr(7,5);//world,从第7位截取5个字符
string s8=s.substr(5);//hello
6、搜索
int index=s.find("world");//6
int index2=s.find('h');//0
index2=s.find('z');
if (index2 == string::npos)//判断不存在
{
cout << "no" << endl;
}
s.rfind('o')//7,逆向搜索
7、插入
s.insert(11,"hello world");//插入开始位置,插入开始位置可以是字符串最后一位的后一位
8、替换
s.replace(6,5,"me");//hello me.将从6开始的之后5个字符替换成me
9、char类型与string
s2 =s+ 'a' + 'b';
string s9{ 'c' };