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,

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

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond