c++ string append常见用法

转载:https://blog.csdn.net/air_wswn/article/details/7785739

C++ string append()添加文本

使用append()添加文本常用方法:

直接添加另一个完整的字符串:

如str1.append(str2);

添加另一个字符串的某一段子串:

如str1.append(str2, 11, 7);

添加几个相同的字符:

如str1.append(5, '.');

注意,个数在前字符在后.上面的代码意思为在str1后面添加5个".".

[cpp]  view plain  copy
  1. //========================================  
  2.   
  3. #include<iostream>  
  4.   
  5. using namespace std;  
  6.   
  7. //========================================  
  8.   
  9. int main()  
  10.   
  11. {  
  12.   
  13.     string str1="I like C++";  
  14.   
  15.     string str2=",I like the world.";  
  16.   
  17.     string str3="Hello";  
  18.   
  19.     string str4("Hi");  
  20.   
  21.     //====================================  
  22.   
  23.     str1.append(str2);  
  24.   
  25.     str3.append(str2, 11, 7);  
  26.   
  27.     str4.append(5, '.');  
  28.   
  29.     //====================================  
  30.   
  31.     cout<<str1<<endl;  
  32.   
  33.     cout<<str3<<endl;  
  34.   
  35.     cout<<str4<<endl;  
  36.   
  37.     system("pause");  
  38.   
  39.     return 0;     
  40.   
  41. }  
  42.   
  43. //========================================  

运行结果为

I like C++,I like the world.

Hello World.

Hi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值