Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate multiple C++ strings on one line?

People also ask

Can we concatenate more than 2 strings in C?

In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination). The pointer of the source string is appended to the end of the destination string, thus concatenating both strings.

How do I concatenate multiple strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is the most efficient way to concatenate many strings together?

If you are concatenating a list of strings, then the preferred way is to use join() as it accepts a list of strings and concatenates them and is most readable in this case. If you are looking for performance, append/join is marginally faster there if you are using extremely long strings.


#include <sstream>
#include <string>

std::stringstream ss;
ss << "Hello, world, " << myInt << niceToSeeYouString;
std::string s = ss.str();

Take a look at this Guru Of The Week article from Herb Sutter: The String Formatters of Manor Farm


In 5 years nobody has mentioned .append?

#include <string>

std::string s;
s.append("Hello world, ");
s.append("nice to see you, ");
s.append("or not.");

s += "Hello world, " + "nice to see you, " + "or not.";

Those character array literals are not C++ std::strings - you need to convert them:

s += string("Hello world, ") + string("nice to see you, ") + string("or not.");

To convert ints (or any other streamable type) you can use a boost lexical_cast or provide your own function:

template <typename T>
string Str( const T & t ) {
   ostringstream os;
   os << t;
   return os.str();
}

You can now say things like:

string s = string("The meaning is ") + Str( 42 );

Your code can be written as1,

s = "Hello world," "nice to see you," "or not."

...but I doubt that's what you're looking for. In your case, you are probably looking for streams:

std::stringstream ss;
ss << "Hello world, " << 42 << "nice to see you.";
std::string s = ss.str();

1 "can be written as" : This only works for string literals. The concatenation is done by the compiler.


Using C++14 user defined literals and std::to_string the code becomes easier.

using namespace std::literals::string_literals;
std::string str;
str += "Hello World, "s + "nice to see you, "s + "or not"s;
str += "Hello World, "s + std::to_string(my_int) + other_string;

Note that concatenating string literals can be done at compile time. Just remove the +.

str += "Hello World, " "nice to see you, " "or not";

In C++20 you'll be able to do:

auto s = std::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);

Until then you could do the same with the {fmt} library:

auto s = fmt::format("{}{}{}", "Hello world, ", myInt, niceToSeeYouString);

Disclaimer: I'm the author of {fmt}.


To offer a solution that is more one-line-ish: A function concat can be implemented to reduce the "classic" stringstream based solution to a single statement. It is based on variadic templates and perfect forwarding.


Usage:

std::string s = concat(someObject, " Hello, ", 42, " I concatenate", anyStreamableType);

Implementation:

void addToStream(std::ostringstream&)
{
}

template<typename T, typename... Args>
void addToStream(std::ostringstream& a_stream, T&& a_value, Args&&... a_args)
{
    a_stream << std::forward<T>(a_value);
    addToStream(a_stream, std::forward<Args>(a_args)...);
}

template<typename... Args>
std::string concat(Args&&... a_args)
{
    std::ostringstream s;
    addToStream(s, std::forward<Args>(a_args)...);
    return s.str();
}