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

How can we swap wide strings? Can I use swap() method to Wide Strings as same as in string?

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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish