How to Concatenate Two Strings in C++

  1. Use += Operator to Concatenate Two Strings in C++
  2. Use append() Method to Concatenate Two Strings in C++

This article will demonstrate multiple methods about 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::cout; using std::endl;
using std::copy; 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. concTwoStrings function constructs a new string object, which is assigned to the string2 variable.

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::copy; 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 builtin 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::cout; using std::endl;
using std::copy; 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::cout; using std::endl;
using std::copy; 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

Related Article - C++ String

  • How to Convert Char Array to String in C++
  • How to Print a String in C++