转载: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个".".
- //========================================
- #include<iostream>
- using namespace std;
- //========================================
- int main()
- {
- string str1="I like C++";
- string str2=",I like the world.";
- string str3="Hello";
- string str4("Hi");
- //====================================
- str1.append(str2);
- str3.append(str2, 11, 7);
- str4.append(5, '.');
- //====================================
- cout<<str1<<endl;
- cout<<str3<<endl;
- cout<<str4<<endl;
- system("pause");
- return 0;
- }
- //========================================
运行结果为
I like C++,I like the world.
Hello World.
Hi