Site icon Learn C++

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,

[crayon-673f5590dec4d906232384/]

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,

[crayon-673f5590dec56121973638/]

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,

[crayon-673f5590dec57405667398/]

See example below,

[crayon-673f5590dec58324736043/]

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

[crayon-673f5590dec5e845338623/]

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:

[crayon-673f5590dec5f014196038/]

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

[crayon-673f5590dec60028443420/]

Output will be as below,

[crayon-673f5590dec63273284686/]

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:

[crayon-673f5590dec64235782980/]

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

[crayon-673f5590dec65941100542/]

Output will be as below,

[crayon-673f5590dec66294459758/]

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:

[crayon-673f5590dec69017839136/]

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

[crayon-673f5590dec6a063161861/]

Output will be as below,

[crayon-673f5590dec6b419753714/]

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.

Exit mobile version