In C++, one of the most used variable types are text strings, also known as alphanumeric variables, and they are really important when storing and retrieving valuable data. It is important to store your data safely in its language and localization. In modern C++, strings are std::basic_string
types such as std::string
, and std::wstring
. In this post, we explain some methods of std::wstring in C++, we will explain copy, assign, append, insert, and replace methods of std::wstring that you can use in string operations.
Table of Contents
What is a basic string (std::basic_string) in C++?
The basic_string (std::basic_string and std::pmr::basic_string) is a class template that stores and manipulates sequences of alpha numeric string objects (char,w_char,…). For example, str::string and std::wstring are the data types defined by the std::basic_string<char>. In other words, basic_string is used to define different data_types which means a basic_string is not a string only, it is a namespace for a general string format. A basic string can be used to define string, wstring, u8string, u16string and u32string data types. Here are all you need to know about std::basic_string,
What is a wide string (std::wstring) in C++?
Wide strings are the string class for wide characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms wstring stores for the alphanumeric text with 2 or 4 byte chars. 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 wstr = L"This is a Wide String\n"; |
Wide 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. Now, let’s see them.
How can we copy a wstring to another wstring in C++?
We can copy wstring to another wstring with assign() method as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::wstring wstr1 = L"This is my string\n"; std::wstring wstr2; wstr2 = wstr1; wstr1 = L"First String is Changed"; std::wcout << wstr2; getchar(); return 0; } |
here are more examples,
How can we assign a wstring to a wstring in C++? (Copying wstring with the assign method)
The assign() method of a wstring, is a method of basic_string ( here it is wstring) assigns a new wide string to the current wide string, it is replacing its contents to contents of a new wide string . Here is the syntax,
1 2 3 |
basic_string& assign (const basic_string& str); |
See example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is my string"; wstr.assign( L"New assigned string"); std::wcout << wstr << "'\n"; getchar(); return 0; } |
or we can copy a wstring to another one by using assign() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::wstring wstr1 = L"This is my string"; std::wstring wstr2 = L"New assigned string"; wstr1.assign(wstr2); // Copy wstr2 to wstr1 std::wcout << wstr1 << "'\n"; getchar(); return 0; } |
How can we use the append method with wstring in C++? (Appending a wstring to a wstring)
We can use append() method to append text with L”…” or to append another wstring as given syntax below.
Syntax:
1 2 3 |
basic_string& append (const basic_string& str); |
This example below appends L” with appended text” to wstr1 and then appends wstr2,
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 wstr1 = L"This is my string"; std::wstring wstr2 = L" and another string"; wstr1.append(L" with appended text"); wstr1.append(wstr2); std::wcout << "Wide String1 = '" << wstr1 << "'\n"; getchar(); return 0; } |
Output will be as below,
1 2 3 |
New String = 'This is my string with appended text and another string' |
Here are more examples about to append one wstring to another.
How can we use the insert method with wstring in C++? (Inserting a wstring to a wstring)
We can use insert() method to insert text with L”…” or to append another wstring as given syntax below.
Syntax:
1 2 3 |
basic_string& insert (size_type pos, const basic_string& str); |
This example below inserts L” amazing” to wstr, and then print outs this wide string,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is C++"; wstr.insert(7, L" amazing"); std::wcout << wstr << '\n'; getchar(); return 0; } |
Output will be as below,
1 2 3 |
This is amazing C++ |
Here are more examples,
How can we use the replace method in wstrings in C++? (Replacing a wstring with a wstring)
We can use replace() method to replace a text with L”…” or to append another wstring as given syntax below.
Syntax:
1 2 3 4 |
basic_string& replace (size_type position, size_type len, const basic_string& str); basic_string& replace (const_iterator iter1, const_iterator iter2, const basic_string& str); |
This example below replace L”amazing” to wstr1 after “This is “, overwrites into “C++” data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is C++"; wstr.replace(8, 8, L"amazing"); std::wcout << wstr << '\n'; getchar(); return 0; } |
Output will be as below,
1 2 3 |
This is amazing |
Here are more examples,
As in given these wstring methods above, these methods can be used on strings methods in the same syntax. These examples can run on C++ Builder, Visual C++, Dev-C++, GNU C++ and other C++ compilers that supports CLANG standards.
There are more string methods and properties in modern c++. Here are all posts about strings: https://learncplusplus.org/?s=string
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.