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

Inserting A String Into A Wide String In A Modern C++ App

Inserting A String Into A Wide String In A Modern C++ App

How can I use insert() method of std::wstring in a modern C++ app? How can I add a string to inside of a wide string? Here are the answers with C++ examples.

Generally, as an introduction to C++, in addition to int, float, double, string there is another data type wstring in modern C++ that we can use for alphanumeric variables. In Modern C++, there are several typedefs for common character types are provided: Wide string types are defined in header <string>.

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 another terms 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 wstring as below,

Other string methods you can use in your C++ app

We’ve written several articles about std:string and Wide string methods. You can read more about them by clicking on this dynamic search link: https://learncplusplus.org/tag/string-methods/

String has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can insert a wide string into another wide string in different methods. Let’s see some of most used methods.

Inserting a Wide String into Another Wide String with insert() Method

insert() method of std::wstring, inserts strings (characters of strings) into a string before the character indicated by insert_position

replaces the part of the wide string indicated by either start and end index positions with a new wide string. That means we can insert string between characters of a wide string. Here is the syntax of a insert() method,

Here is an example about inserting a wide string into another widestring,

and the output will be,

Inserting a String into a Wide String with the insert() Method

In this insert() method of the std::wstring class, we can only use wide strings, to add strings or chars we must convert them to wstring. Here is the full example that shows how we can insert a string into a wstring;

as you see here, to create a new ws widestring we used s.begin() and s.end() methods to show the range of string to be converted to the wstring.

Inserting a Char array into a Wide String in a C++ app with the insert() Method

As same for strings, In this insert() method of the std::wstring class, we can add chars. To do this, first we must convert them to wstring. Here is the full example that shows how we can insert a char array into a wstring;

as you see here, to create a new ws widestring we used &s[0] and &s[ strlen(s) ] addresses to point the range of string to be converted to the wstring.

Inserting a Wide String into another Wide string at a specific position with Start, using the insert() method

insert() method of std::wstring, inserts strings (characters of strings) into a string before the character indicated by insert_position and the start poistion

here is the full example,

Inserting a Wide String into another Wide string in range by using the insert() method

Here is the full example,

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

Who Invented The C++ Programming Language?

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?