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

How To Use Basic Methods Of Wide Strings In C++?

How To Use Basic Methods Of Wide Strings In C++

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.

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,

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,

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,

See example below,

or we can copy a wstring to another one by using assign() method:

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:

This example below appends L” with appended text” to wstr1 and then appends wstr2,

Output will be as below,

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:

This example below inserts L” amazing” to wstr, and then print outs this wide string,

Output will be as below,

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:

This example below replace L”amazing” to wstr1 after “This is “, overwrites into “C++” data.

Output will be as below,

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

How To Use Basic Methods Of Wide Strings In C++ C++ logo

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.

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++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?