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,
1 2 3 |
std::wstring str = L"This is a String"; |
Table of Contents
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,
1 2 3 |
basic_string& insert (size_type insert_position, const basic_string& str); |
Here is an example about inserting a wide string into another widestring,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"the first wide string "; s2 = L"-and the second-"; s1.insert( 10, s2 ); // Inserts s2 to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
the first -and the second-wide string |
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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1 = L"the first wide string "; std::string s = "-This is a string-"; std::wstring ws( s.begin(), s.end() ); // let's convert string to a wide string s1.insert( 10, ws ); // Inserts s2 to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1 = L"the first wide string "; char s[] = "-This is a char array-"; std::wstring ws( &s[0], &s[strlen(s)] ); // let's convert char array to a wide string s1.insert( 10, ws ); // Inserts s2 to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
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
1 2 3 |
basic_string& insert (size_type insert_position, const basic_string& str, size_type sub_position); |
here is the full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"the first wide string "; s2 = L"-and the second-"; s1.insert( 10, s2, 4); // Inserts s2 starting from 4th char to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
Inserting a Wide String into another Wide string in range by using the insert() method
1 2 3 |
basic_string& insert (size_type insert_position, const basic_string& str, size_type sub_position, , size_type sublen); |
Here is the full example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"the first wide string "; s2 = L"-and the second-"; s1.insert( 10, s2, 4, 4 ); // Inserts 4 chars of s2 starting from 4th char to the position 10 of the s1 std::wcout << s1 << std::endl; getchar(); return 0; } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition