Site icon 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,

[crayon-6741a5ae3de0a439235807/]

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,

[crayon-6741a5ae3de11136426136/]

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

[crayon-6741a5ae3de14312464722/]

and the output will be,

[crayon-6741a5ae3de15540789392/]

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;

[crayon-6741a5ae3de16354816649/]

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;

[crayon-6741a5ae3de17120723528/]

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

[crayon-6741a5ae3de18422922378/]

here is the full example,

[crayon-6741a5ae3de19859309911/]

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

[crayon-6741a5ae3de1a152462237/]

Here is the full example,

[crayon-6741a5ae3de1b839138893/]
Exit mobile version