In C++, std::string::append() adds characters to the end of an existing string. This tutorial explains how to append another string, append multiple strings, add part of a string, append characters, and append numeric values, with examples of append(), +=, +, and push_back().
Append Text to a C++ std::string
To append text to a std::string, call its append() member function. The supplied characters are added to the end of the existing string, and the original string is modified.
For example, if str1 contains "Hello" and str2 contains " World", calling str1.append(str2) changes str1 to "Hello World".
C++ string::append() Syntax
The syntax of string.append() function to append string str2 to string str1 is
str1.append(str2);
The function returns a reference to the modified string. Because of this, calls to append() can be chained when several strings need to be added in sequence.
str1.append(str2).append(str3);
Common C++ string::append() Forms
std::string::append() has several overloads. These let you append an entire string, a C-style string, part of another string, or repeated copies of a character.
| Operation | Example | What is appended |
|---|---|---|
| Append another string | str1.append(str2) | All characters in str2 |
| Append a string literal | str1.append(" World") | Characters in the null-terminated string |
| Append part of another string | str1.append(str2, pos, count) | Up to count characters beginning at pos |
| Append repeated characters | str1.append(3, '!') | Three ! characters |
C++ String Append Example with Another std::string
In the following program, we take two strings: str1 and str2 and append str2 to str1 using string.append() function.
main.cpp
#include <iostream>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = " World";
str1.append(str2);
cout << str1 << endl;
}
Output
Hello World
Program ended with exit code: 0
The call to str1.append(str2) modifies str1. The value of str2 itself is not changed.
Append Multiple Strings in C++ with Chained append()
In the following program, we take three strings: str1 , str2 and str3 and append str2 and str3 respectively to str1 using string.append() function in a single statement using chaining mechanism.
main.cpp
#include <iostream>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = " World.";
string str3 = " Welcome!";
str1.append(str2).append(str3);
cout << str1 << endl;
}
Output
Hello World. Welcome!
Program ended with exit code: 0
The first call appends str2 and returns the modified str1. The next append(str3) therefore continues appending to the same string.
Append a String Literal or C-Style String in C++
You do not have to create a second std::string when the text to append is already available as a string literal or null-terminated C-style string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello";
text.append(" World");
cout << text << endl;
return 0;
}
Output
Hello World
Append Part of Another String in C++
An overload of append() accepts a source string, a starting position, and a character count. This is useful when only a portion of another string should be added.
destination.append(source, position, count);
In the following example, the first three characters of source are appended to result.
#include <iostream>
#include <string>
using namespace std;
int main() {
string result = "Learn ";
string source = "C++ Programming";
result.append(source, 0, 3);
cout << result << endl;
return 0;
}
Output
Learn C++
String positions are zero-based, so position 0 refers to the first character. If the requested count extends beyond the end of the source string, the available characters from the starting position are appended.
Append a Character to a C++ String
To append one character, push_back() is a direct choice. You can also use +=. The append() function can add a character by specifying how many copies of that character should be inserted.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello";
text.push_back('!');
text.append(2, '!');
cout << text << endl;
return 0;
}
Output
Hello!!!
push_back('!') adds one character. Then append(2, '!') appends two more copies of the same character.
C++ append() vs push_back() for Characters
append() and push_back() serve different common cases. Use push_back() when adding one character. Use append() when adding a string, a character sequence, a substring, or multiple copies of one character.
| Requirement | Typical operation |
|---|---|
| Add one character | text.push_back(ch) |
| Add another string | text.append(other) |
| Add several copies of one character | text.append(count, ch) |
| Add part of another string | text.append(other, pos, count) |
Append an Integer or Other Number to a C++ String
std::string::append() does not format an integer as decimal text automatically. Convert the numeric value to a string first. For standard numeric types, std::to_string() is a convenient option.
#include <iostream>
#include <string>
using namespace std;
int main() {
int score = 42;
string message = "Score: ";
message.append(to_string(score));
cout << message << endl;
return 0;
}
Output
Score: 42
The call to to_string(score) creates the text "42", which can then be appended normally.
C++ string append() vs += and + Operators
C++ provides several ways to concatenate strings. append() and += modify an existing string, while + is commonly used to form a concatenated result as an expression.
| Method | Example | Effect |
|---|---|---|
append() | a.append(b) | Appends to a and provides overloads for substrings and repeated characters. |
+= | a += b | Appends to a with concise syntax. |
+ | c = a + b | Forms a concatenated string result. |
push_back() | a.push_back(ch) | Adds one character to the end of a. |
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "Hello";
string b = " World";
string withPlus = a + b;
a += b;
cout << withPlus << endl;
cout << a << endl;
return 0;
}
Output
Hello World
Hello World
For straightforward concatenation, any of these approaches may be appropriate. append() becomes particularly useful when you need one of its overloads, such as appending only a selected range of characters.
Appending Repeated Characters with string::append()
The overload append(count, character) appends the specified number of copies of a character. This can be useful for separators, padding, and other generated text.
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
line.append(10, '-');
cout << line << endl;
return 0;
}
Output
----------
String Capacity When Performing Many C++ Appends
A std::string manages storage for its characters. As text is appended, the string may need to obtain a larger storage area and move its existing characters. The exact capacity-growth strategy is implementation-dependent.
If you know approximately how large the final string will be, reserve() can request enough capacity in advance. This can reduce the need for repeated reallocations while building a large string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
text.reserve(50);
text.append("C++");
text.append(" string");
text.append(" append");
cout << text << endl;
return 0;
}
Output
C++ string append
The work required by an append depends on factors such as how many characters are added and whether the string needs to grow its storage. For ordinary code, prefer the clearest operation first; consider capacity management when repeated appends are significant for the workload.
Important Rules When Using C++ string::append()
append()adds characters at the end of the current string.- The destination
std::stringis modified by the call. append()returns a reference to the modified string, which allows chained calls.- Use the substring overload when only a selected part of another string should be appended.
- Use
append(count, ch)to add several copies of one character. - For a single character,
push_back()is usually the clearest dedicated operation. - Convert an integer or other numeric value to text before appending it as text.
- Use
reserve()when building a large string through many appends and the approximate final size is known.
C++ String Append Summary
In this C++ Tutorial, we learned how to append text with std::string::append(). We also covered chained appends, string literals, substrings, repeated characters, single-character appends, numeric conversion with std::to_string(), and the differences between append(), +=, +, and push_back().
TutorialKart.com