Concatenate strings in C++
This post will discuss how to concatenate strings in C++.
1. Using std::ostringstream
A simple solution to concatenate strings is using the std::ostream::operator<< function. The following solution demonstrates this by applying the insertion operator << to an output stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <sstream> int main() { std::string s1 = "Hello"; std::string s2 = "World"; std::ostringstream ss; ss << s1 << " " << s2; std::string s = ss.str(); std::cout << s << std::endl; // Hello World return 0; } |
2. Using string::append
To append additional strings at the end of an existing string, we can use the string::append member function. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> int main() { std::string s1 = "Hello"; std::string s2 = "World"; std::string s; s.append(s1).append(" ").append(s2); std::cout << s << std::endl; // Hello World return 0; } |
3. Using Implicit concatenation
In C++, any adjacent strings are joined together into a single string by the compiler. This is known as the implicit string concatenation. The following C++ program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <string> int main() { std::string s = "Hello" "World"; std::cout << s << std::endl; // HelloWorld return 0; } |
4. Using string concat
Another common approach to concatenate multiple string objects together is using the concat (+) operator. Here’s how the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> int main() { std::string s1 = "Hello"; std::string s2 = "World"; std::string s = s1 + " " + s2; std::cout << s << std::endl; // Hello World return 0; } |
C++14 allows to forms a string literal of the various type using std::literals::string_literals::operator""s. These operators are declared in the namespace std::literals::string_literals. For example, the following code combines the string literals with standard s-suffix.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> using namespace std::literals::string_literals; int main() { std::string str; str += "Hello"s + " "s + "World"s; std::cout << str << std::endl; // Hello World return 0; } |
5. Using std::format
Starting with C++20, we can use the formatting library that offers a safe and extensible alternative to the printf family of functions. To concatenate multiple strings, we can use the std::format defined in header <format>, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> #include <format> int main() { auto str = std::format("{}{}{}", "Hello", " ", "World"); std::cout << str << std::endl; // Hello World return 0; } |
That’s all about concatenating strings in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)