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++

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.

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.

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.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

C++C++14C++17C++20Learn C++

What Are The C++14 Features Removed From C++17?

C++C++11C++14C++17C++20Learn C++Syntax

What is the conjunction (std::conjunction) metafunction in C++?