C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ String erase()This function removes the characters as specified, reducing its length by one. Syntax
Consider a string str. Syntax would be: str.erase(pos,len); str.erase(itr); str.erase(first,last); Parameter
Return value
It returns *this. Example 1Let's see a simple example when pos and len are given:
#include<iostream>
using namespace std;
int main()
{
string str="This is a java tutorial";
str.erase(8,1);
cout<<str;
return 0;
}
Output: This is java tutorial Example 2Let's see a simple example when iterator is passed in a parameter:
#include<iostream>
using namespace std;
int main()
{
string str="java programming";
str.erase(str.begin()+11);
cout<<str;
return 0;
}
Output: java programing Example 3Let's see a simple example when the range is mentioned in a parameter:
#include<iostream>
using namespace std;
int main()
{
string str="This is an example of C and C++";
str.erase(str.begin()+24,str.end());
cout<<str;
return 0;
}
Output: This is an example of C
Next TopicC++ Strings
|