Concatenation

suggest change

You can concatenate std::strings using the overloaded + and += operators. Using the + operator:

std::string hello = "Hello";
std::string world = "world";
std::string helloworld = hello + world; // "Helloworld"

Using the += operator:

std::string hello = "Hello";
std::string world = "world";
hello += world; // "Helloworld"

You can also append C strings, including string literals:

std::string hello = "Hello";
std::string world = "world";
const char *comma = ", ";
std::string newhelloworld = hello + comma + world + "!"; // "Hello, world!"

You can also use push_back() to push back individual chars:

std::string s = "a, b, ";
s.push_back('c'); // "a, b, c"

There is also append(), which is pretty much like +=:

std::string app = "test and ";
app.append("test"); // "test and test"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


std::string:
* Concatenation

Table Of Contents
8 Arrays
11 Loops
20 std::string
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype