#include<iostream>
#include<string>
using namespace std;
void test1()
{
string s1=" hello ";; //重载+=操作符
s1+="world";
cout<<"s1 = "<<s1<<endl;
s1+='!'; //重载+=操作符
cout<<"s1 = "<<s1<<endl;
string str="--你好,世界!"; //重载+=操作符
s1+=str;
cout<<"s1 = "<<s1<<endl;
string s2="I"; //把字符串s连接到当前字符串结尾
(" love");
cout<<"s2 = "<<s2<<endl;
(" you qwer",3); //把字符串s的前n个字符连接到当前字符串结尾
cout<<"s2 = "<<s2<<endl;
(s1); //同operator+=(const string &str)
cout<<"s2 = "<<s2<<endl;
string str1="我来了! werfwffsd";
(str1,0,7); //每个汉字两个字节,叹号一个字节:”我来了!“一共七个字节,所以是0-7。string &append(const string &s,int pos,int n);字符串s中从pos开始的n个字符连接到字符串结尾
cout<<"s2 = "<<s2<<endl;
}
int main()
{
test1();
return 0;
}
3.字符串添加单个元素
string a="123";
1.在字符串末尾添加一个字符
a.push_back('3'); //结果为 a="1233";
2.在字符串末尾删除一个字符
a.pop_back(); //结果为 a="12";