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,


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.

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
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

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

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