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 @   纸包鱼  阅读(1040)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示