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

原文链接: https://www.cnblogs.com/uestc-du/p/16245517.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ string字符串拼接

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/189065

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午2:36
下一篇 2023年2月12日 下午2:36

相关推荐