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.
Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.
As you know, the best way to concatenate two strings in C programming is by using the strcat() function.
concat() With concat , you can create a new string by calling the method on a string.
const string message = "Hello" + ",world" + exclam;
The +
operator has left-to-right associativity, so the equivalent parenthesized expression is:
const string message = (("Hello" + ",world") + exclam);
As you can see, the two string literals "Hello"
and ",world"
are "added" first, hence the error.
One of the first two strings being concatenated must be a std::string
object:
const string message = string("Hello") + ",world" + exclam;
Alternatively, you can force the second +
to be evaluated first by parenthesizing that part of the expression:
const string message = "Hello" + (",world" + exclam);
It makes sense that your first example (hello + ",world" + "!"
) works because the std::string
(hello
) is one of the arguments to the leftmost +
. That +
is evaluated, the result is a std::string
object with the concatenated string, and that resulting std::string
is then concatenated with the "!"
.
As for why you can't concatenate two string literals using +
, it is because a string literal is just an array of characters (a const char [N]
where N
is the length of the string plus one, for the null terminator). When you use an array in most contexts, it is converted into a pointer to its initial element.
So, when you try to do "Hello" + ",world"
, what you're really trying to do is add two const char*
s together, which isn't possible (what would it mean to add two pointers together?) and if it was it wouldn't do what you wanted it to do.
Note that you can concatenate string literals by placing them next to each other; for example, the following two are equivalent:
"Hello" ",world"
"Hello,world"
This is useful if you have a long string literal that you want to break up onto multiple lines. They have to be string literals, though: this won't work with const char*
pointers or const char[N]
arrays.
You should always pay attention to types.
Although they all seem like strings, "Hello"
and ",world"
are literals.
And in your example, exclam
is a std::string
object.
C++ has an operator overload that takes a std::string
object and adds another string to it. When you concatenate a std::string
object with a literal it will make the appropriate casting for the literal.
But if you try to concatenate two literals, the compiler won't be able to find an operator that takes two literals.
Your second example does not work because there is no operator +
for two string literals. Note that a string literal is not of type string
, but instead is of type const char *
. Your second example will work if you revise it like this:
const string message = string("Hello") + ",world" + exclam;
Since C++14 you can use two real string literals:
const string hello = "Hello"s;
const string message = hello + ",world"s + "!"s;
or
const string exclam = "!"s;
const string message = "Hello"s + ",world"s + exclam;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With