Concatenating Strings – append() or + Operator
We can concatenate string in C++ using two methods:
1. + Operator
The + operator is overloaded in the std::string class to perform string concatenation.
Syntax
string_object1 + string_object2
Example
std::string firstName = "Geeks"; std::string lastName = "forGeeks"; std::string fullName = firstName + " " + lastName;
+ operator is used to concatenate two strings. The string fullName will be “w3wiki”.
2. append()
The append() function is another member function to concatenate two strings.
Syntax
string_object1.append(string2)
Parameters
- string2: This function takes the string to be appended as a parameter. It can be both C or C++ Style string.
Return Value
- Reference to the final string.
std::string base = "Hey! Geeks"; base.append(" Welcome to w3wiki!"); // Append a string
The append() function adds one string to the end of another.
String Functions In C++
A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this article, will look at the functions of string computations.
Contact Us