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

How To Swap One Wide String With Another Wide String

How To Swap One Wide String With Another Wide String hero image

In this post, you’ll discover how can we swap Wide Strings? Can I apply the swap() method to Wide Strings in the same way that I would to strings?
By learning the wide strings & swap () method to wide strings, it will help you build C++ applications with the use of the C++ app.

Generally, as an introduction to C++, in addition to int, float, double there is another data type, wstring that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>. You can read more about Wide Strings here too: https://learncplusplus.org/it/learn-to-use-wide-strings-wstring-in-c/

What is a Wide String?

wstrings are the string class for byte 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-byte chars called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In C++ simply we can define a string as below,

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. Here are the string types defined in std namespace with their char type and C++ standard.

wstring has methods to append, assign, copy, align, replace or operate with other wide strings. These methods can be used in all string methods with their appropriate syntax. We can swap a wide string with another wide string with swap() method. Let’s see this method.

How to Swap a Wide String with Another Wide String in C++

Swap Method swap(), exchanges the content of the one wide string by the content of another wide string, generally in C++ string types presented with basic_string object of the same type and the Syntax for the swap method can be displayed as below,

wstrings are the string class for byte 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-byte chars called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In C++ , we can display a swap method as below,

Syntax:

We can swap two wide strings by using one of its swap() method. For example we can use swap() method of wstring wstr1 with another wstring wstr2 as below,

or we can use swap() method of wstring wstr2 with another wstring wstr1 as below,

these both usage will result with same. Wide String lengths may be different.

Use the Swap method to exchange Wide strings

In another terms, the value of content is exchanged with the value of the another content by calling swap method. In general, swap method can be used as in this example,

and the Wide String outputs will be,

or we can use method of second wide string, both results will be same;

and the Wide String outputs will be,

A summark of Wide Strings

As you see we can swap the content of wide strings easily by using this swap() method, it is very fast and easy, compatible with all CLANG C++ compilers like C++ Builder, Dev-C++, GNU C/C++, Visual C++, and some other C++ compilers.

Note that we can also use this swap() method with string in C++. Swapping other types i.e. wstring to string or string to wstring may require more conversion techniques and also may cause to loss of wide characters. Because in that situation, we try to convert each 2-bytes char to single-byte ASCII chars, that means we will lose wide chars higher than 255. Generally, this is not recommended, or check your strings about possible char losses before you do this.

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.

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++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?