DevAcademia lesson/C++ Basics
String Concatenation
Topic progressTopic 26 of 109 - 24% complete
~8 min lesson15 learning blocks4 code examples1 checks/interactions
Lesson outline
Combining Strings
Concatenation means joining strings together. In C++, you can concatenate strings using the + operator.
Appending Strings
You can also append to strings using the += operator or the append() method.
Concatenation Rules
- You can concatenate
std::stringobjects with otherstd::stringobjects. - You can concatenate
std::stringobjects with C-style strings (string literals orconst char*). - You can concatenate
std::stringobjects with single characters (e.g.,'!'). - You cannot use
+between two string literals (both operands areconst char*), but adjacent string literals without+are concatenated at compile time.
Efficiency Tips
Each operator+ creates a new string. Prefer +=/append() in loops and consider reserving capacity to reduce reallocations.
Was this topic clear?