代码
#include<iostream>#include<string>using namespace std;//string字符串拼接/*① string& operator+=(const char* str); //重载+=操作符② string& operator+=(const char c); //重载+=操作符③ string& operator+=(const string& str); //重载+=操作符④ string& append(const char *s); //把字符串s连接到当前字符串结尾⑤ string& append(const char *s,int //把字符串s的前n个字符连接到当前字符串结尾⑥ string& append(const string &s); //同operator+=(const string& str)⑦ string& append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾*/void test01(){ string s1 = "我";//① s1 += "不服"; cout << "s1 = " << s1 << endl; s1 += ';';//中文字符不行//② cout << "s1 = " << s1 << endl; string s2 = "NO !";//③ s1 += s2; cout << "s1 = " << s1 << endl; string s3 = "Never";//④ s3.append(" Give"); cout << "s3 = " << s3 << endl; s3.append(" Up aasfdfgf", 3);//⑤ cout << "s3 = " << s3 << endl; s3.append(s1);//⑥ cout << "s3 = " << s3 << endl; s3.append(s2, 2, 2);//⑦ cout << "s3 = " << s3 << endl;}int main() { test01(); return 0;}
运行结果
