.append()
Published Jun 23, 2025
Contribute to Docs
In C++, .append()
is a method that concatenates new characters to the end of an existing string without requiring reassignment. It can be used to combine two different strings, append a portion of a string (also known as a substring), or add specific characters to the end of a string.
Syntax
str.append(str2); // Appends entire string
str.append(str2, pos, len); // Appends substring from str2
str.append(n, char); // Appends character 'char' n times
str.append(first, last); // Appends range using iterators
Parameters:
str2
: The string to append or extract a substring from.pos
: The starting index instr2
(used when appending a substring).len
: The number of characters to append fromstr2
, starting atpos
.n
: The number of times to append the characterchar
.char
: A character to be appended multiple times.first
,last
: Iterators defining a range of characters to append.
Return value:
Returns a reference to the modified string (*this
), allowing method chaining.
Example 1: Appending Two Strings Together
This example takes two separate strings and concatenates them:
#include <iostream>#include <string>using namespace std;int main () {string stringOne = "Hello ";string stringTwo = "World!";//Appends stringTwo to the end of stringOnestringOne.append(stringTwo);cout << stringOne;return 0;}
The output produced by this code is:
Hello World!
Example 2: Appending One Piece of a String to Another
This example takes a portion of one string and adds it to the end of another string:
#include <iostream>#include <string>using namespace std;int main() {string str1("Straw Hat ");string str2("Monkey D. Luffy");// Appends 5 characters from index 10 of str2 to str1str1.append(str2, 10, 5);cout << str1;return 0;}
The output produced by this code is:
Straw Hat Luffy
Codebyte Example: Appending Multiple Characters to a String
This codebyte example takes a specific character and adds it a specific amount of times to the end of a string:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours