How can I clear a string in a simple way? What kind of methods I can use in my C++ software to clear a std::string? How can I use clear() method with strings? Can I use resize() method to clear strings? How can I use erase() method to clear strings?
Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std::string types that are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In other terms, string stores the alphanumeric text with 1-byte chars called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
Strings (string) are a class contains arrays of characters with useful methods, and we can access, or modify their characters easily. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘.
The string 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 string by using clear() methods.
Table of Contents
How to clear the contents of a string with the clear() Method in a C++ app?
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::string str = "This is a String"; str.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::string str = "This is a String"; std::cout << str << std::endl; str.clear(); std::cout << str << std::endl; getchar(); return 0; } |
How do I clear a string by setting String to “” in C++ software?
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::string str = "This is a String"; std::cout << str << std::endl; str = ""; std::cout << str << std::endl; getchar(); return 0; } |
How to clear the contents of a string with the C++ resize() method?
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 = "This is a String"; std::cout << str << std::endl; str.resize(0); std::cout << str << std::endl; getchar(); return 0; } |
How do I clear a 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 = "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; } |
A comparison of Clear Methods of Strings in C++
We can compare all those clear methods in an example. Here we prepared a use str_info() function to display string 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 |
#include <iostream> #include <cassert> #include <string> void str_info(std::string s) { auto cap = s.capacity(); auto size = s.size(); auto length = s.length(); auto max_size = s.max_size(); std::cout << "String : " << s << std::endl; std::cout << " length : " << length << std::endl; std::cout << " size : " << size << std::endl; std::cout << " max-size: " << max_size << std::endl; std::cout << " capacity: " << cap << std::endl; std::cout << " empty : "; if(s.empty()) std::cout << "Yes" << std::endl; else std::cout << "No" << std::endl; std::cout << std::endl; } int main() { std::cout << "Definition Only\n"; std::string str; str_info(str); str = "This is a String"; std::cout << "Defined String Value\n"; str_info(str); std::cout << "Using clear() method:\n"; str = "This is a String"; str.clear(); str_info(str); std::cout << "Using "" :\n"; str = "This is a String"; str = ""; str_info(str); std::cout << "Using resize() method :\n"; str = "This is a String"; str.resize(0); str_info(str); std::cout << "Using erase() method :\n"; str = "This is a String"; str.erase(str.begin(), str.end()); str_info(str); getchar(); return 0; } |