此函数用于通过在当前值的末尾追加来扩展字符串。
Str1.append(str2); Str1.append(str2,pos,len); Str1.append(str2,n);
str - 字符串对象,该对象将附加到另一个字符串对象中。
pos - 它确定要附加到另一个对象的第一个字符的位置。
len - 要在另一个字符串对象中作为子字符串复制的字符数。
n - 要复制的字符数。
此函数不返回任何值。
让我们看一下将字符串追加到另一个字符串对象中的示例。
#include<iostream> using namespace std; int main() { string str1="Welcome to C++ programming"; string str2="language"; cout<<"Before appending,string value is"<<str1<<'\n'; str1.append(str2); cout<<"After appending, string value is"<<str1<<'\n'; return 0; }
输出:
Before appending,string value is Welcome to C++ programming After appending,string value is Welcome to C++ programming language
让我们看一下通过使用位置和长度作为参数附加字符串的示例。
#include<iostream> using namespace std; int main() { string str1 = "Mango is my favourite" ; string str2 ="fruit"; cout<<"Before appending, string value is :" <<str1<<'\n'; str1.append(str2,0,5); cout<<"After appending, string value is :" <<str1<<'\n'; return 0; }
输出:
Before appending, string value is Mango is my favourite After appending, string value is Mango is my favourite fruit
让我们来看另一个例子。
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-string-append-function.html
来源:LearnFk无涯教程网
#include<iostream> using namespace std; int main() { string str1 = "Kashmir is nature"; str1.append("of beauty",9) ; cout<<"String value is :"<<str1; return 0; }
输出:
String value is Kashmir is nature of beauty
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)