C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Add Characters to Wide Strings in Modern C++

This Is How To Add Characters to Wide Strings in Modern C++

How can I add a char to a wide string? How can I remove char from a string? How can I use push_back() method in strings? Can I use push_back() and pop_back() methods in std::string as same as in std::wstring?

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,

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.

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,

Adding a Character by Using += Operator

Here is full example that adds ‘X’ and ‘Y’ characters to a wstring,

The wide string outputs will be like this

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;

Here is how our example output looks

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:

We can add characters with push_back() method by giving the character number as a parameter. See example below,

and the output will be same as before,

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:

In C++, we can add characters by using insert() method by giving iterator i and the character number c. See example below,

and the output will be same as before,

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:

Here is the full example for the append() method of wstring

and the output will be

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.

Let’s see how we can use single character wide strings to add a wide string.

this is not wrong but wide string forms are slower than using wide character forms, and the output will be

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish