← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 9 of 15

C++ String Concatenation

std::string supports concatenation with the + and += operators, letting strings and even individual characters be joined together directly without calling a special function.
Syntax
cpp
std::string result = string1 + string2;

string1 += string2;

Concatenating with +

The + operator, overloaded for std::string, joins two strings together into a new combined string, working just as naturally as adding two numbers.

Example: Concatenating with +

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string first = "Hello, ";
	std::string second = "world!";
	std::string result = first + second;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Concatenating with +=

The += operator appends one string onto the end of another in place, modifying the original string variable rather than creating a brand new one.

Example: Concatenating with +=

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string message = "Hello";
	message += ", world!";
	// Print to the console with cout
	std::cout << message << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Concatenating Multiple Strings

Several strings and literals can be chained together with + in a single expression, and C++ evaluates the concatenation left to right, building up the final combined string.

Example: Concatenating Multiple Strings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string result = "A" + std::string("B") + "C" + "D";
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Appending Characters

A single char can be appended to a std::string with += just like another string, letting characters be built up one at a time inside a loop.

Example: Appending Characters

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string word = "";
	word += 'H';
	word += 'i';
	// Print to the console with cout
	std::cout << word << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Mixing Strings and Non-String Values

The + operator cannot directly concatenate a std::string with a raw number; the number must first be converted to a string, commonly with std::to_string, before it can be joined in.

Example: Mixing Strings and Non-String Values

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	std::string message = "Age: " + std::to_string(age);
	// Print to the console with cout
	std::cout << message << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Adding two string literals like "a" + "b", which does not compile because both are const char*.
  2. Using + in a loop for huge strings, which is slower than append.
  3. Concatenating a number with + and getting garbage, when you should use std::to_string.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.