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,
1 2 3 |
std::string wstr = L"This is a String"; |
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.
Table of Contents
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:
1 2 3 |
void pop_back(); |
We can remove the last character with pop_back() method without using any parameter. Here is the example below,
1 2 3 4 |
std::string str = "This is a String"; str.pop_back(); |
This deletes the last character of the string str. See full example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::wstring ws1 = L"This is a Wide String"; std::wcout << ws1 << std::endl; ws1.pop_back(); std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This is a Wide String This is a Wide Strin |
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:
1 2 3 |
basic_string& erase (index i); |
We can remove the last character with erease() as in the example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::wstring ws1 = L"This is a Wide String"; std::wcout << ws1 << std::endl; ws1.erase( ws1.length()-1 ); std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the outputs will be,
1 2 3 4 |
This is a Wide String This is a Wide Strin |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::wstring ws1 = L"This_is_a_Wide_String"; std::wcout << ws1 << std::endl; ws1.erase( 4 ); std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This_is_a_Wide_String This |
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,
1 2 3 |
basic_string& erase (size_type pos = 0, size_type len = npos); |
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:
1 2 3 |
basic_string& resize (size_type n); |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() std::wstring ws1 = L"This_is_a_Wide_String"; std::wcout << ws1 << std::endl; ws1.resize( ws1.length()-6 ); std::wcout << ws1 << std::endl; getchar(); return 0; } |
and the wide string outputs will be,
1 2 3 4 |
This_is_a_Wide_String This_is_a_Wide_ |
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