How can I use erase() method of std::wstring? How can I erase part of a wide string? Here are the answers with modern C++ app examples.
Generally, as an introduction to C++, in addition to int, float, double, string there is another data type called wide string that we use for alphanumeric variables in Modern C++. In Modern C++, there are several typedefs for common character types are provided: Wide String types are defined in header <string>.
Table of Contents
What is a Wide String in C++?
Wide Strings (wstrings) are the string class for byte characters represented with wstring and alphanumeric characters are stored and displayed in string forms. In another terms, wstring stores for the alphanumeric text with 2-byte chars called wide chars (wchar_t). Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a string as below,
1 2 3 |
std::wstring str = L"This is a Wide String"; |
wstring has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can erase part of a string by using erase() method. Let’s see this method now.
How do I erase part of a Wide String with the erase() C++ app method?
erase() method of std::string in C++ removes string characters from the string in a given range. We can directly give the start position in a string to erase all behind as given Syntax below
1 2 3 |
basic_string& erase( size_type start_position); // Until C++20 |
or we can erase inside of a string by using start position to delete and number of characters. Thus, erase() method will delete all characters in this given Syntax below.
1 2 3 |
basic_string& erase( size_type start_position, size_type char_count); // Until C++20 |
Here is a C++ erase() example that uses both erase() methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> int main() { std::wstring str; str = L"This is my string in C++"; str.erase(4); std::wcout << str << std::endl; str = L"This is my string in C++"; str.erase(7, 13); std::wcout << str << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 4 |
This This is C++ |
As you see in the first part we delete all after the 4th character and in the second part we delete 13 characters after 7th character,
Why do I get “Out of Range” when erasing a Wide String in C++?
As given example above in this method, we can directly copy, assign, replace or insert any string after its some part being erased. When we use erase() method, we must be sure that the first parameter is lower than the string size (str.size()) and the second parameter is lower than size()-first_parameter, which means both should be inside the string. If second parameter exceeds the string size, it returns remaining string till the position without throwing an exception. If the first parameter exceed the limits of string (size of string) then this erase method will throw out_of_range, we can catch this exception as given example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> int main() { std::wstring str; str = L"This is my string in C++"; try { str.erase(7, 13); std::wcout << str << std::endl; } catch(const std::out_of_range& e) { std::wcout << L"out of range, please check string size\n"; } getchar(); return 0; } |
In this example above, we try to erase 2 characters of this given string starting from 64th character which means that we exceed the limits of this string size, and the output will be,
1 2 3 |
This is C++ |
if you set first parameter of erase to a higher value than the length of wide string, at this situation you will have this exception below,
1 2 3 |
Out of range, please check string size |
As you see it is very easy to erase any part of a string, and this erase() method can be used with std::strings as given examples above.