Home C++ Strings Functions append() C++ std::string append() Function Definition In C++, std::string::append() adds characters onto the end of the string.
You can append a whole string, a C-string, a repeated character, or a piece of another string. Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax Start with these common calls.
s.append(" world");
s.append(3, '!');
s.append(other, 0, 2); Call What it does s.append(str)Append all of str. s.append(cstr)Append a null-terminated C-string. s.append(n, ch)Append character ch repeated n times.
s += "x" often calls append under the hood. Prefer the form that reads clearest.
Library declarations string& append(const string& str);
string& append(const char* s);
string& append(size_type n, char ch);
string& append(const string& str, size_type pos, size_type n); Parameters Parameter Description str / sText to append. n, chRepeat count and character. posStart index in the source string for substring overloads.
Return Value Return Value Description string&Reference to *this, so you can chain calls.
C++ Examples 1. Append string 2. Append chars 3. Append part Example 1 — Basic append() Append a word.
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
s.append(" C++");
std::cout << s << '\n';
return 0;
} How It Works Start with "Hello". Append adds " C++" at the end. Printed result combines both. Example 2 — Append Repeated Char Add three dots.
#include <iostream>
#include <string>
int main() {
std::string s = "Wait";
s.append(3, '.');
std::cout << s << '\n';
return 0;
} How It Works append(3, '.') adds three periods.Size grows by 3. Useful for padding or simple decoration. Example 3 — Append a Substring Append part of another string.
#include <iostream>
#include <string>
int main() {
std::string a = "Code";
std::string b = "ToFunExtra";
a.append(b, 0, 5);
std::cout << a << '\n';
return 0;
} How It Works We take 5 characters from b starting at 0: "ToFun". They are appended to a. Result is "CodeToFun". Frequently Asked Questions What does append do? It adds characters to the end of the string.
How is append different from +=? += is shorthand; append offers more overloads such as repeat counts and substrings.
Does append return the string? Yes, a reference to *this for chaining.
Can append throw? Yes, for example std::bad_alloc if allocation fails, or out_of_range for bad substring positions.
What about append_range? C++23 adds append_range to append a whole range of characters.
Next: append_range() Keep going through the string functions. Append all characters from a range (C++23).
append_range() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion