The short answer
CONCATENATE() is a spreadsheet function. It joins text from several cells or values into one piece of text. In Excel or Google Sheets, this formula returns Hello, world!:
=CONCATENATE("Hello, ", "world!")
People use it to build a label, a name, or a code from separate parts, such as a first name in one cell and a last name in another.
C++ has no CONCATENATE() function
The C++ standard library has no function named CONCATENATE(). Code that needs to join text uses other tools:
- The
+and+=operators onstd::string. - The
append()member function ofstd::string. - The
strcatfamily in<cstring>, for C-style strings.
The name strcat may sound like a spreadsheet function, but it comes from C and works differently. It does not create a new string. It changes the char array you pass in, so that array must be large enough for the result.
The C++ version of a spreadsheet join
Suppose two cells hold a first name and a last name. In a spreadsheet, you might write:
=CONCATENATE(A1, " ", B1)
In C++, the same join uses std::string values:
#include <iostream>
#include <string>
int main() {
std::string first = "Ada";
std::string last = "Lovelace";
std::string full_name = first + " " + last;
std::cout << full_name << '\n'; // Ada Lovelace
}
Each + joins the text so far with the next piece, from left to right. The space is its own piece, so the code adds it explicitly.
Joining a number
Spreadsheets usually convert numbers to text for you. In C++, convert a number with std::to_string before you join it:
int count = 3;
std::string label = std::to_string(count) + " items"; // "3 items"
For more ways to convert numbers, see c convert int to string.
When this matters
If you learned to join text in a spreadsheet, the idea carries over to C++. You still combine pieces of text. In code, you write each join explicitly, which makes the result easy to check with a print statement or a test.
For the general idea behind joining text, see what is string concatenation. For the full set of options in C++, see how to concatenate strings in c.