How can I use the append() method of std::string? How can I append a string to another string in a C++ app? Can we append a char array to a string? How can I append a number of chars to a string? Is it possible to add a substring of a string to another string? Here are the answers with C++ examples.
Table of Contents
A little word about C++ strings
Generally, as an introduction to C++, in addition to int, float, double there is another data type, string that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In another terms string stores for the alphanumeric text with 1 byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
String has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can append a string to another string in different methods.
How to append a string to another string in C++ with the += Operator
We can use ‘+=’ operator to append strings as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first string "; s2 = "second string "; s1 += s2; // Appending string s2 to string s1 std::cout << s1 << std::endl; getchar(); return 0; } |
Did you know you can append a string to another string with the C++ append() method?
The append() method of std::string appends additional characters to the string. Here is the syntax of append() method.
1 2 3 |
basic_string& append( const basic_string& str ); |
As given in this syntax, we can append a string to another string with append() method as in example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "first string "; s2 = "second string "; s1.append(s2); // Appending string s2 to string s1 std::cout << s1 << std::endl; getchar(); return 0; } |
This is how to append a Char Array to a string in a C++ app
We can append a char array to a string with append() method as given above. Here is the syntax of append() method.
1 2 3 |
basic_string& append( const basic_string& str ); |
We can add char array to a string and then we can append a new string as in example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> int main() { char s[]="char string "; std::string s1,s2; s1 = "first string "; s2 = "second string "; s1.append(s); // Appending string s2 to char array s1 s1.append(s2); // Appending string s2 to string s1 std::cout << s1 << std::endl; getchar(); return 0; } |
In C++ how can I append a number of repeated characters to a string?
The append() method appends count
copies of character to a string as given syntax below (until C++20).
1 2 3 |
basic_string& append( size_type count, CharT ch ); |
Here is an example that appends charcters in given number to a string, see example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s1,s2; s1 = "first string "; s1.append( 5, '-' ); // Appending 5 '-' char to string s1 std::cout << s1 << std::endl; getchar(); return 0; } |
How to append a substring to a string in C++
The append() method of std::string can be used to append a substring (with pos, pos+count)
to a string. If the requested substring lasts past the end of the string, or if count == npos, the appended substring is [pos, size())
. If pos > str.size(), std::out_of_range is thrown. (since C++14, until C++20). Here is a Syntax below,
1 2 3 |
basic_string& append( const basic_string& str, size_type pos, size_type count ); |
Here is a example to append a substring of a string to a string,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s1,s2; s1 = "first string "; s2 = "another string "; s1.append( s2, 2, 5 ); // Appending s2 starting from 3th char to 8th chars to the string s1 std::cout << s1 << std::endl; getchar(); return 0; } |
There are many other ways to use append methods(). Note that we can also use these append() methods to append wstring in C++.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition