String Clear Function - C++ Programming Tutorial
C++ Course / Strings / String Clear Function

String Clear Function

BLUF: Mastering String Clear Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Clear Function

C++ is renowned for its efficiency. Learn how String Clear Function enables low-level control and high-performance computing in the tutorial below.

C++ String clear

This function eliminates the elements and results in an empty string.

Syntax

Consider a string str. Syntax would be:

Example

str.clear();

Parameter

This function does not contain any parameter.

Return value

This function does not return any value.

Example

Let's see a simple example of clear function:

Example

#include<iostream>
using namespace std;
int main()
{
stringstr;
cout<<?Enter some lines about javaCppTutorial and write dot character to finish the sentence?<<?\n?;
cout<<?If you want to clear the text, press d?<<?\n?;
do{
charch =cin.get();
str+=ch;
if(ch==?d?)
str.clear();
}while(ch!=?.?);
return 0;
}

Output:

Output

Enter some lines about javaCppTutorial and write dot character to finish the sentence
If you want to clear the text, press d

This example shows how clear function works.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience