使用C ++ STL中的string :: append()函数将文本追加到字符串
append()是<string>标头的库函数,用于在字符串中附加多余的字符/文本。
语法:
string& append(const string& substr);
这里,
string&是对要在其中添加额外字符的字符串的引用。
substr是要附加的字符/子字符串的额外集合。
程序中还将使用该函数的其他一些变体(函数重载)。
程序:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str = "Hello";
string str2 = " ## ";
string str3 = "www.google.com";
cout<<str<<endl;
//在末尾附加文字
str.append (" world");
cout<<str<<endl;
//附加'str2'
str.append (str2) ;
cout<<str<<endl;
//追加空间
str.append (" ");
//从str3附加'google'
str.append (str3.begin () +4, str3.begin () +10) ;
cout<<str<<endl;
return 0;
}输出结果
Hello
Hello world
Hello world ##
Hello world ## google