c++ 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 n); //把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s); //operator+=(const string& str)
string& append(const string &s, int pos, int n); //字符串s中从pos开始的n个字符连接到字符串结尾

 

  string s1 = "hello";
  cout << s1 <<endl;
  s1 += " world";
  cout << s1 <<endl;
  s1 += 's';
  cout << s1 <<endl;
  string s2 = " wanvei";
  s1 += s2;
  cout << s1 <<endl;
  s1.append(" is");
  cout << s1 <<endl;
  s1.append("kkkkkk",4);
  cout << s1 <<endl;
  s1.append(s2);
  cout << s1 <<endl;
  s1.append(s2,4,3);
  cout << s1 <<endl;
hello
hello world
hello worlds
hello worlds wanvei
hello worlds wanvei is
hello worlds wanvei iskkkk
hello worlds wanvei iskkkk wanvei
hello worlds wanvei iskkkk wanveivei
posted @ 2022-05-08 14:12  纸包鱼  阅读(1051)  评论(0)    收藏  举报