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::wstring str = "This is a String"; |
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.
In this post, you’ll learn how to clear a wide string in a straightforward manner, as well as the various methods to use to clear a std::wstring, such as using the clear() method with wstrings; the resize() method to clear strings, and the erase() method to clear strings. By learning how to use wide string clear methods in c++, it will help you to easily build C++ applications using the C++ Compiler.
Table of Contents
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:
1 2 3 |
void clear() noexcept; |
We can simply clear characters of a string by using its clear() method as below.
1 2 3 4 |
std::wstring wstr = L"This is a String"; wstr.clear(); |
Here we can clear characters of a string by using its clear() method as given full example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is a String"; std::wcout << wstr << std::endl; str.clear(); std::wcout << wstr << std::endl; getchar(); return 0; } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::wstring wstr = L"This is a String"; std::wcout << wstr << std::endl; wstr = L""; std::wcout << wstr << std::endl; getchar(); return 0; } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> int main() { std::string str = L"This is a String"; std::cout << str << std::endl; str.resize(0); std::cout << str << std::endl; getchar(); return 0; } |
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.
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::string str = L"This is a String"; std::cout << str << std::endl; str.erase(str.begin(), str.end()); std::cout << str << std::endl; std::cout << str << std::endl; getchar(); return 0; } |
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;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <iostream> #include <cassert> #include <string> //------------------------------------------------------------------------------ void wstr_info(std::wstring ws) { auto cap = ws.capacity(); auto size = ws.size(); auto length = ws.length(); auto max_size = ws.max_size(); std::wcout << L"WideString: " << ws << std::endl; std::wcout << L" length : " << length << std::endl; std::wcout << L" size : " << size << std::endl; std::wcout << L" max-size: " << max_size << std::endl; std::wcout << L" capacity: " << cap << std::endl; std::wcout << L" empty : "; if(ws.empty()) std::wcout << L"Yes" << std::endl; else std::wcout << L"No" << std::endl; std::wcout << std::endl; } //------------------------------------------------------------------------------ int main() { std::wcout << L"Wide String Capacity Methods and Properties\n"; std::wcout << L"Definition Only\n"; std::wstring wstr; wstr_info(wstr); wstr = L"This is a Wide String"; std::wcout << L"Defined String Value\n"; wstr_info(wstr); std::wcout << L"Using reserve() method:\n"; wstr = L"This is a Wide String"; wstr.reserve(50); wstr_info(wstr); std::wcout << L"Using clear() method:\n"; wstr = L"This is a Wide String"; wstr.clear(); wstr_info(wstr); std::cout << L"Using "" :\n"; wstr = L"This is a Wide String"; wstr = L""; wstr_info(wstr); std::wcout << L"Using resize() method :\n"; wstr = L"This is a Wide String"; wstr.resize(0); wstr_info(wstr); std::wcout << L"Using erase() method :\n"; wstr = L"This is a Wide String"; wstr.erase(wstr.begin(), wstr.end()); wstr_info(wstr); std::wcout << L"Using reserve() method :\n"; wstr.reserve(50); wstr_info(wstr); std::wcout << L"Using chrink_to_fit() method :\n"; wstr.shrink_to_fit(); wstr_info(wstr); getchar(); return 0; } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition