How can I use the append() method of std::wstring? How can I append or join a wide string to another wide string? Can we append a char array to a wide string? How can I append number of chars to a wide string? Is it possible to add a substring of a string to another string? Here are the answers with C++ software examples.
Generally, as an introduction to C++, in addition to int, float, double, string there is another string data type called wstring that we use for alphanumeric variables with alphanumeric characters. In Modern C++ there are several typedefs for common character types are provided: wstring types are defined in header <string>.
Table of Contents
What is a wstring in C++ software?
wstrings (Wide Strings) are the string class for characters that has 2 bytes that represented with wstring. In Modern C++ alphanumeric characters are stored in wide strings because of their supports to characters of world languages and displayed in wstring forms. In other words, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t or WChar. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a string as below,
1 2 3 |
std::wstring wstr = L"This is a Wide String\n"; |
wstring has methods to assign, copy, align, replace or to operate with other wstrings as same strings. These methods can be used in all wstring methods with their appropriate syntax. We can append a wstring to another wstring in different methods.
How to get your C++ software to append a wide string to another wide string with the += operator
We can use ‘+=’ operator to append wide strings as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"the first wide string "; s2 = L"and the second wide string"; s1 += s2; // Appending wide string s2 to wide string s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be as follows,
1 2 3 |
the first wide string and the second wide string |
Appending a wide string to another wide String with append() method
append() method of std::wstring appends additional characters to the wide 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 20 21 22 |
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cwchar> int main() { std::wstring s1, s2; s1 = L"the first wide string "; s2 = L"and the second wide string"; s1.append(s2); // Appending wide string s2 to wide string s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be as follows,
1 2 3 |
the first wide string and the second wide string |
How to get your C++ software to append a Char array to a wide string
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 |
#include <iostream> #include <string> int main() { std::wstring s1,s2; s1 = L"the first wide string "; s2 = L"and the second wide string"; s1.append( 5, L'-' ); // Appending 5 '-' wide char to string s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be as follows,
1 2 3 |
the first wide string and the second wide string |
Appending a wide character in Given Number
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 characters 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() { char s[]="char string "; char c = "x"; std::string s1; s1 = "The string is "; s1.append(4, c); // Appending four "x" characters std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be as follows,
1 2 3 |
the string is xxxx |
Appending a substring to a wide String
append() method of std::wstring can be used to append a substring (with pos, pos+count)
to a string. If the requested substring lasts past the end of the wstring, 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 wide string to a wide string,
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::wstring s1,s2; s1 = L"the first wide string "; s2 = L"and the second wide string"; s1.append( s2, 2, 5 ); // Appending s2 starting from 3th char to 8th chars to the string s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be as follows,
1 2 3 |
the first wide string and the second wide string |
There are many other ways to use append methods(). Note that we can also use these append() methods to append wstring in C++.