Home »
C++ STL
Appending text to the string in C++ STL
C++ STL: string::append() function with example: In this tutorial, we will learn to append text to the string using string::append() function.
Submitted by IncludeHelp, on July 02, 2018
C++ STL - string::append() Function
append() is a library function of <string> header, it is used to append the extra characters/text in the string.
Syntax
string& append(const string& substr);
Parameter(s)
- string& is the reference to the string in which we are adding the extra characters.
- substr is the extra set of character/sub-string to be appended.
There are some other variations (function overloading) of the function that are going to be used in the program.
Example for appending text to the string in C++ STL
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
string str2 = " ## ";
string str3 = "www.google.com";
cout << str << endl;
// append text at the end
str.append(" world");
cout << str << endl;
// append 'str2'
str.append(str2);
cout << str << endl;
// append space
str.append(" ");
// append 'google' from str3
str.append(str3.begin() + 4, str3.begin() + 10);
cout << str << endl;
return 0;
}
Output
Hello
Hello world
Hello world ##
Hello world ## google