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

3 Ways To Remove Characters From WString In A Modern C++ App

How can I remove a character from a wide string in my C++ app? How can I use pop_back() method in wide strings? Can I use erase() and resize() methods in std::wstring to remove characters?

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,

The 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 add characters with + or += operators ; we can use push_back() and insert() methods to add chars to a wide string. Let’s see how we add and remove characters from wide strings.

How to remove a character from a wide string in Modern C++

Wide Strings are class which has arrays of wide characters and methods, and we can add and remove characters them easily. In Modern C++, while wide string contents are defined between L” and “, characters are defined between L’ and ‘ with the literal L. We can delete the last character of a wide string by using the pop_back() method .

Using pop_back() method to remove a character from a wide string

The best and easy way to delete the last character of a wstring is to use the pop_back() method.

pop_back() method is a String Method that removes the last character of the basic_string which means it is also decreasing its length by one. Here is the general syntax of the pop_back() method of wstring.

Syntax:

We can remove the last character with pop_back() method without using any parameter. Here is the example below,

This deletes the last character of the string str. See full example below,

and the string outputs will be,

as you see we delete the last character of this wide string.

Using the erase() method to delete characters from a wide string

erase() method is a String Method that deletes the last characters from the given index of the basic_string which means it is also decreasing its length by the deleted number of characters. Here is the general syntax of the erase() method of wstring.

Syntax:

We can remove the last character with erease() as in the example below,

and the outputs will be,

as you see we delete the last character of this wide string by pointing the index ws1.length()-1 . We can also delete more characters after the index given by using this erase() method . For example we can delete characters after the 4th char of a wide string as below,

and the string outputs will be,

We can also use this erase method to delete portion of string which means we can delete characters in a given range inside of a string. Here is the Syntax below,

Using the resize() method to delete characters from a wide String

resize() method is a String Method that defines the new size of the basic_string which means it can be used to delete extra characters if they are not needed. . Here is the general syntax of the erase() method to string.

Syntax:

We can remove the last characters with resize(). For example we can delete the last 6 characters of a wide string as in the example below,

and the wide string outputs will be,

We can use all pop_back(), erase() and resize() methods of a std::string safely in our C++ applications. Note that we can use these methods on std::wstrings in our modern C++ applications

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

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

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond