A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/stdstringclear-in-cpp/ below:

std::string::clear in C++ - GeeksforGeeks

std::string::clear in C++

Last Updated : 23 Jul, 2025

The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters.

Parameters:

none

Return Value:

none

void string ::clear ()
- Removes all characters (makes string empty)
- Doesn't throw any error
- Receives no parameters and returns nothing
CPP
// CPP code to illustrate
// clear() function

#include <iostream>
#include <string>
using namespace std;

// Function to demo clear()
void clearDemo(string str)
{
    // Deletes all characters in string
    str.clear();

    cout << "After clear : ";
    cout << str;
}

// Driver code
int main()
{
    string str("Hello World!");

    cout << "Before clear : ";
    cout << str << endl;
    clearDemo(str);

    return 0;
}
Output:
Before clear : Hello World!
After clear : 
Related Article: std::string::erase

If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using

write.geeksforgeeks.org

or mail your article to review-team@geeksforgeeks.org.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4