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

猜你喜欢

转载自blog.csdn.net/qq_32511517/article/details/80791314