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.....
本文介绍了C++中使用string类的append()方法来追加文本的几种方式,包括追加完整字符串、追加子串及重复字符。通过示例代码展示了如何实现这些功能,并给出了运行结果。

1万+

被折叠的 条评论
为什么被折叠?



