C++ string append() Function
Example
Add a string to the end of another string:
string firstName = "John ";
string lastName = "Doe";
firstName.append(lastName);
cout << firstName;
Definition and Usage
The append() function adds text to the end of a string. It changes the original string.
You can add another string, or a number of copies of one character, like txt.append(3, '!').
The append function is defined in the <string> header file.
Tip: You can also use the += operator to add text to the end of a string, like firstName += lastName;.
Syntax
string.append(str);
string.append(size_t count, char c);
Use one of the two forms. The size_t data type is a non-negative integer.
Parameter Values
| Parameter | Description |
|---|---|
| str | Required. The string to add. |
| count | Required. The number of times to add the character. |
| c | Required. The character to add. |
Technical Details
| Returns: | A reference to the changed string. |
|---|
Related Pages
Read more about strings in our String Tutorial.