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

Learn How To Use Wide String Clear Methods in C++

How can I clear a wide string in a simple way? What kind of methods I can use to clear a std::wstring? How can I use clear() method with wstrings? Can I use resize() method to clear strings? How can I use erase() method to clear strings?

Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std::wstring types that are defined in header <string>.

wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other 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 modern C++, simply we can define a wide string with L” ” literal as below,

Wide Strings (wstring) are a class that contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. In C++, while wide string contents are defined between L” and ” with literal L, wide characters are defined between L’ and ‘ with literal L

The wstring 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 clear a wstring with clear()erase(), resize() methods. 

How to clear a Wide String with the clear() method in C++

The clear() method is a String Method that removes all characters from the string as same as setting string to “”. It doesn’t have parameter and it doesn’t return a value; After using clear() method pointers, references, and iterators related with it are invalidated.

Syntax:

We can simply clear characters of a string by using its clear() method as below.

Here we can clear characters of a string by using its clear() method as given full example below,

How to clear a Wide String by setting it to “” in C++

In Modern C++, Simply we can clear a string by setting it to “”, Here is an example below,

How to clear a Wide String with the resize() Method in C++

We can use resize() method to set string size. We can use 0 to clear it, here is an example below,

How to clear a Wide String with the erase() method in C++

We can clear a string by using erase() method with begin() and end() iterators as it’s parameters.

Comparing Clear Methods of Wide String in C++

We can compare all those clear methods in an example. Here we prepared a use wstr_info() function to display wstring details after each operation. We can use empty() method if the string is empty or not. Here is the full example to see each methods;

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++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

ru_RURussian