C++ std::string append() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

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.

C++
s.append(" world");
s.append(3, '!');
s.append(other, 0, 2);
CallWhat 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
C++
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

ParameterDescription
str / sText to append.
n, chRepeat count and character.
posStart index in the source string for substring overloads.

Return Value

Return ValueDescription
string&Reference to *this, so you can chain calls.

C++ Examples

Example 1 — Basic append()

Append a word.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";
    s.append(" C++");
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Start with "Hello".
  2. Append adds " C++" at the end.
  3. Printed result combines both.

Example 2 — Append Repeated Char

Add three dots.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Wait";
    s.append(3, '.');
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. append(3, '.') adds three periods.
  2. Size grows by 3.
  3. Useful for padding or simple decoration.

Example 3 — Append a Substring

Append part of another string.

C++
#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

  1. We take 5 characters from b starting at 0: "ToFun".
  2. They are appended to a.
  3. Result is "CodeToFun".

Frequently Asked Questions

It adds characters to the end of the string.
+= is shorthand; append offers more overloads such as repeat counts and substrings.
Yes, a reference to *this for chaining.
Yes, for example std::bad_alloc if allocation fails, or out_of_range for bad substring positions.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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