Site icon 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,

[crayon-676368c7e7793106006190/]

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:

[crayon-676368c7e779b050601975/]

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,

[crayon-676368c7e779c662005600/]

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

[crayon-676368c7e779d868780796/]

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,

[crayon-676368c7e779e742680659/]

and the string outputs will be,

[crayon-676368c7e779f732316415/]

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

[crayon-676368c7e77a0912403944/]

and the string outputs will be,

[crayon-676368c7e77a1308378651/]

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.

Exit mobile version