string& operator+=(const char* str);
//重载+=操作符
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1=" << str1 << endl;
string& operator+=(const char c);
//重载+=操作符
str1 += ':';
cout << "str1=" << str1 << endl;
string& operator+=(const string& str);
//重载+=操作符
string str2 = "原神启动";
str1 += str2;
cout << "str1=" << str1 << endl;
string& append(const char *s);
//把字符串 s 连接到当前字符串结尾
string str3 = "I";
str3.append(" Love");
cout << "str3=" << str3 << endl;//I Love
string& append(const char *s, int n);
//把字符串 s 的前 n 个字符连接到当前字符串结尾
string str3 = "I love";
//n表示追加的字符串长度
str3.append(" games abcde", 6);
cout << "str3=" << str3 << endl;//I Love games
string& append(const string &s);
//同 operator+=(const string& str)
string str3 = "I Love games"
string str2 = "原神启动";
str3.append(str2);
cout << "str3=" << str3 << endl;//I Love games原神启动
string& append(const string &s, int pos);
//字符串 s 中从 pos 开始到末尾的字符连接到字符串结尾
string str3 = "I Love games原神启动";
string str4 = "LOL genshin";
//追加字符串的一部分,从第n个索引位开始追加
str3.append(str4, 3);
cout << "str3=" << str3 << endl;//I Love games原神启动 genshin
string& append(const string &s, int pos, int n);
//字符串s中从pos开始的n个字符连接到字符串结尾
如果是中文的话,n 要乘以 2(一个中文占两 Byte)
string str3 = "I Love games原神启动 genshin"
string str4 = "LOL genshin";
//从第0个索引位开始追加长度为3的字符串
str3.append(str4, 0, 3);
cout << "str3=" << str3 << endl; //I Love games原神启动 genshinLOL
#include<iostream>
#include<string>
using namespace std;
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1=" << str1 << endl;
str1 += ':';
cout << "str1=" << str1 << endl;
string str2 = "原神启动";
str1 += str2;
cout << "str1=" << str1 << endl;
//append()
string str3 = "I";
str3.append(" Love");
cout << "str3=" << str3 << endl;//I Love
//n表示追加的字符串长度
str3.append(" games abcde", 6);
cout << "str3=" << str3 << endl;//I Love games
str3.append(str2);
cout << "str3=" << str3 << endl;//I Love games原神启动
string str4 = "LOL genshin";
//追加字符串的一部分,从第n个索引位开始追加
str3.append(str4, 3);
cout << "str3=" << str3 << endl;//I Love games原神启动 genshin
//从第0个索引位开始追加长度为3的字符串
str3.append(str4, 0, 3);
cout << "str3=" << str3 << endl;
}
int main()
{
test01();
return 0;
}