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

This Is How To Swap One String With Another In A C++ App

This Is How To Swap One String With Another In A C++ App

How can I swap strings? Can I use swap() method to std::string in the same ways as I can in std::wstring in a C++ App?

Generally, in Introduction to C++ lessons, examples are started with string examples and end with them, while Modern C++ uses Strings, Wide Strings and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std:string types that are defined in header <string>.

strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other words, string stores for alphanumeric text are 1-byte chars, called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,

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

This is how to swap one string with another string in C++

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

Syntax:

We can swap two strings by using one of its swap() method. For example we can use swap() method of string s1 with another string s2 as below,

or we can use swap() method of string s2 with another string s1 as below,

these both will produce the same result. Here string lengths may be different.

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

and the string outputs will be,

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

and the string outputs will be,

As you see we can swap the content of 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 std::wstring 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, swap of these types 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++Learn C++

How To Make Your C++ Compiler Fly Much Faster

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

This Is How To Join Two Wide Strings In Modern C++ Software

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

How To Use The All-Powerful swscanf function In Your C++ App

C++ComponentsLanguage FeatureLearn C++

How To Use A Sharpen Effect in C+ Software On Windows?