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

This Is How To Join Two Wide Strings In Modern C++ Software

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

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,

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,

and the output will be as follows,

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.

As given in this syntax, we can append a string to another string with append() method as in example below,

and the output will be as follows,

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.

We can add char array to a string and then we can append a new string as in example below,

and the output will be as follows,

Appending a wide character in Given Number

append() method appends count copies of character to a string as given syntax below (until C++20).

Here is an example that appends characters in given number to a string, see example below,

and the output will be as follows,

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,

Here is a example to append a substring of a wide string to a wide string,

and the output will be as follows,

There are many other ways to use append methods(). Note that we can also use these append() methods to append wstring in C++.


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

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
CC++C++17Introduction to C++Language FeatureLearn C++

What Is Count In C Programming?

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

Why Is C The Most Popular Programming Language?

CC++C++17Introduction to C++Learn C++Syntax

How To Use Switch In C++ And C Programming?

CC++C++17Introduction to C++Learn C++

What Are Header Files In C++ and C Programming?