代码

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. //string字符串拼接
  5. /*
  6. ① string& operator+=(const char* str); //重载+=操作符
  7. ② string& operator+=(const char c); //重载+=操作符
  8. ③ string& operator+=(const string& str); //重载+=操作符
  9. ④ string& append(const char *s); //把字符串s连接到当前字符串结尾
  10. ⑤ string& append(const char *s,int //把字符串s的前n个字符连接到当前字符串结尾
  11. ⑥ string& append(const string &s); //同operator+=(const string& str)
  12. ⑦ string& append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
  13. */
  14. void test01(){
  15. string s1 = "我";//①
  16. s1 += "不服";
  17. cout << "s1 = " << s1 << endl;
  18. s1 += ';';//中文字符不行//②
  19. cout << "s1 = " << s1 << endl;
  20. string s2 = "NO !";//③
  21. s1 += s2;
  22. cout << "s1 = " << s1 << endl;
  23. string s3 = "Never";//④
  24. s3.append(" Give");
  25. cout << "s3 = " << s3 << endl;
  26. s3.append(" Up aasfdfgf", 3);//⑤
  27. cout << "s3 = " << s3 << endl;
  28. s3.append(s1);//⑥
  29. cout << "s3 = " << s3 << endl;
  30. s3.append(s2, 2, 2);//⑦
  31. cout << "s3 = " << s3 << endl;
  32. }
  33. int main() {
  34. test01();
  35. return 0;
  36. }

运行结果

image.png