Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std:wstring types that are defined in header <string>.
wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other terms, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In modern C++, simply we can define a wide string with L” ” literal as below,
1 2 3 |
std::string wstr = L"This is a String"; |
wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax. We can add characters with + or += operators ; we can use push_back() and insert() methods to add chars to a wide string. Let’s see how we add and remove characters to wide strings.
In this post, you’ll learn how to add a character to a wide string; how to remove a character from a string; how to use the push_back() method in strings, and how to use the push_back() and pop_back() methods in std::string and std::string. By learning these character elements of a c++ string, it will help you build C++ applications with c++ IDE windows.
Table of Contents
How to Add a Character to a Wide String in Modern C++
Wide Strings (std::wstring) are arrays of characters, and we can add characters. In C++, while wstring contents are defined between ” and ” chars are defined between ‘ and ‘. So we can add a character by typing it between the ‘ and the ‘ with L literal. For example we can define a wstring and we can add a ‘!’ character as below,
1 2 3 4 |
std::wstring wstr = L"This is a String"; wstr += L'!'; |
Adding a Character by Using += Operator
Here is full example that adds ‘X’ and ‘Y’ characters to a wstring,
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 = L"Characters Added to Wide String:"; s1 += L'X'; std::wcout << s1 << std::endl; s1 += L'Y'; std::wcout << s1 << std::endl; getchar(); return 0; } |
The wide string outputs will be like this
1 2 3 4 |
Characters Added to Wide String:X Characters Added to Wide String:XY |
We can add characters in their ASCII Character numbers. For example upper case letters starts from 65, Lets try to add some chars with a for loop as 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 ws1 = L"Characters Added to String:"; for(int i=65; i<=75; i++) { ws1 += (wchar_t)(i); std::wcout << ws1 << std::endl; } getchar(); return 0; } |
Here is how our example output looks
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Characters Added to Wide String:A Characters Added to Wide String:AB Characters Added to Wide String:ABC Characters Added to Wide String:ABCD Characters Added to Wide String:ABCDE Characters Added to Wide String:ABCDEF Characters Added to Wide String:ABCDEFG Characters Added to Wide String:ABCDEFGH Characters Added to Wide String:ABCDEFGHI Characters Added to Wide String:ABCDEFGHIJ Characters Added to Wide String:ABCDEFGHIJK |
Adding a Character by Using push_back() Method of Strings
push_back() method is a String Method that appends a character to the end of the basic_string which means it is also increasing its length by one. Here is the general syntax of the push_back() method to wstring.
Syntax:
1 2 3 |
void push_back (charT c); |
We can add characters with push_back() method by giving the character number as a parameter. See 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 ws1 = L"Characters Added to Wide String:"; for(int i=65; i<=75; i++) { ws1.push_back(i); std::wcout << ws1 << std::endl; } getchar(); return 0; } |
and the output will be same as before,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Characters Added to Wide String:A Characters Added to Wide String:AB Characters Added to Wide String:ABC Characters Added to Wide String:ABCD Characters Added to Wide String:ABCDE Characters Added to Wide String:ABCDEF Characters Added to Wide String:ABCDEFG Characters Added to Wide String:ABCDEFGH Characters Added to Wide String:ABCDEFGHI Characters Added to Wide String:ABCDEFGHIJ Characters Added to Wide String:ABCDEFGHIJK |
Adding a Character by Using insert() Method of Strings
insert() method of string is a String Method that inserts character or wide string to the basic_string which means it is also increasing its length. We can use begin() or end() iterators to add characters. Here is the general syntax of the insert() method to wstring.
Syntax:
1 2 3 |
iterator insert (iterator iter, charT c); |
In C++, we can add characters by using insert() method by giving iterator i and the character number c. See 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 ws1 = L":Characters Added to Wide String:"; ws1.insert(ws1.begin(), L'X'); std::wcout << ws1 << std::endl; ws1.insert(ws1.end(), L'Y'); std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the output will be same as before,
1 2 3 4 |
X:Characters Added to Wide String: X:Characters Added to Wide String:Y |
Adding a Character by Using append() Method of Strings
append() method of string is a String Method that appends constant character arrays or wide string to the basic_string which means it is also increasing its length. Append method needs const charT* as a parameter. Here is the general syntax of the append() method of wstring.
Syntax:
1 2 3 |
basic_string& append (const charT* s); |
Here is the full example for the append() method of wstring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> int main() { std::wstring ws1 = L":Characters Added to Wide String:"; const wchar_t wc1[]= L"X"; const wchar_t wc2[]= L"Y"; ws1.append(wc1); std::wcout << ws1 << std::endl; ws1.append(wc2); std::wcout << ws1 << std::endl; std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the output will be
1 2 3 4 |
Characters Added to Wide String:X Characters Added to Wide String:XY |
Adding a Character by Using Single Character Wide Strings
Note that you can also add characters by using single character wide strings like L”X”, L”Y”. We don’t recommend this, professionally not a good way. Because adding a wide string is slower than adding a wide char. Hence, try to use L’ and ‘ instead of L” and “. For example those both lines prints out ‘/n’ character to the end which is about the same with std::endl. Here the first line is using wide string and the second one is using wide char which is the correct way and faster.
1 2 3 4 |
std::wcout << L"\n"; std::wcout << L'\n'; |
Let’s see how we can use single character wide strings to add a wide string.
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 ws1 = L"Characters Added to Wide String:"; ws1 += L"X"; std::wcout << ws1 << std::endl; ws1 += L"Y"; std::wcout << ws1 << std::endl; getchar(); return 0; } |
this is not wrong but wide string forms are slower than using wide character forms, and the output will be
1 2 3 4 |
Characters Added to Wide String:X Characters Added to Wide String:XY |
We hope you like different ways of adding wstrings. Note that these same methods can be used with std::strings too. You can use the blog search function to find other articles about wstrings in this series.