How to Concatenate Two Strings in C++

Jinku Hu Feb 02, 2024
  1. Use += Operator to Concatenate Two Strings in C++
  2. Use append() Method to Concatenate Two Strings in C++
How to Concatenate Two Strings in C++

This article will demonstrate multiple methods of how to concatenate two strings in C++.

Use += Operator to Concatenate Two Strings in C++

std::string type is mutable and natively supports = and += operators, the latter of which directly translates into in-place string concatenation. This operator can be used to concatenate a string type variable, a string literal, a C-style string, or a character to a string object. The following example shows two string variables getting appended to each other and outputted to the console.

#include <iostream>
#include <string>

using std::copy;
using std::cout;
using std::endl;
using std::string;

int main() {
  string string1("Starting string ");
  string string2("end of the string ");

  cout << "string1:  " << string1 << endl;
  string1 += string2;
  cout << "string1:  " << string1 << endl;

  return EXIT_SUCCESS;
}

Output:

string1:  Starting string
string1:  Starting string end of the string

Alternatively, we can construct a custom function that takes two string variables as parameters and returns the result of concatenation. Note that string has a move constructor, so returning long strings by value is quite efficient. The concTwoStrings function constructs a new string object, which is assigned to the string2 variable.

#include <iostream>
#include <string>

using std::copy;
using std::cout;
using std::endl;
using std::string;

string concTwoStrings(const string& s1, const string& s2) { return s1 + s2; }

int main() {
  string string1("Starting string ");

  string string2 = concTwoStrings(string1, " conc two strings");
  cout << "string2: " << string2 << endl;

  return EXIT_SUCCESS;
}

Output:

string2: Starting string  conc two strings

Use append() Method to Concatenate Two Strings in C++

append is the built-in method of the std::string class. It offers rich functionality, all of which can be explored on its manual page. In this case, we utilize it to concatenate a literal string value to the string object.

#include <iostream>
#include <string>

using std::copy;
using std::cout;
using std::endl;
using std::string;

int main() {
  string string("Temporary string");

  string.append(" appended sequence");
  cout << string << endl;

  return EXIT_SUCCESS;
}

Output:

Temporary string appended sequence

append method returns a pointer to this object so you can make multiple chained function calls and append to a string variable several times. This method can also append initializer list of characters with the following syntax: append({ 'a', 'b', 'c', 'd'}).

#include <iostream>
#include <string>

using std::copy;
using std::cout;
using std::endl;
using std::string;

int main() {
  string string1("Starting strings");
  string string2("end of the string");

  string1.append(" ").append(string2).append("\n");
  cout << string1;

  return EXIT_SUCCESS;
}

Output:

Starting string end of the string
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ String