String:: erase() function in C++

In this tutorial, we are going to discuss the erase() function in C++ and its different form of syntax and uses which are helpful in solving problems of string easily during solving competitive programming problem.

String:: erase() function in C++

erase() function erases a part of string and shortens the length of string. erase() returns this* pointer.

  • To erase all characters in a given string

Syntax :-                        string_name.erase();

string.erase()

string s;
   cin>>s;

  s.erase(); // This will erase all the contents in the given string.
  • To erase all characters after a certain position

Syntax :-                          string_name.erase(index);

All the characters after the index value will be deleted.

string.erase(index)

string s;
 cin>s;

     s.erase(2) // It will delete all characters after index 2
  • Deleting a certain number of functions after a certain index

Syntax :-                                    string_name.erase(index, value);

It will delete the value number of characters after index given in parameter.

string.erase(index, value)

string s;

   cin>>s;

s.erase(1,3); // It will delete 3 characters after index 1
  • Deleting a character after a certain position. If position is not found the iterator will return to the string.end() position which is the hypothetical position after the last position in a string.

Syntax :-              string_name.erase(position+value);

string.erase(position+value)

string s;
    cin>>s;

   s.erase(s.begin()+value);
  • Deleting characters between a certain range in a string.

Syntax:-                            string_name(starting range, ending range);

string(starting range, ending range)

string s;
 cin>>s;

          s.erase( s.begin()+1, s.erase()-3);

C++ Code implementation of erase() function in different forms

#include <iostream>
using namespace std;

  int main()
{
  string s="Codespeedy";
  
      s.erase();
     
        cout<<"Your string is deleted: "<<s<<endl;
        
         s="Codespeedy";
         
          s.erase(1);
           
           cout<<"Your string is deleted after character 1: "<<s<<endl;
           
              s="Codespeedy";
              
                   s.erase(s.begin()+3);
              
                cout<<" 3rd index characters from beginning is deleted: "<<s<<endl;
                
                s="Codespeedy";
                
                  s.erase(s.begin()+2, s.end()-2);
                  
                    cout<<"Characters between 2nd index from starting and 2nd position is deleted: "<<s<<endl;
                    
                    
                    
               
                      
}

OUTPUT

Your string is deleted: 
Your string is deleted after character 1: C
 3rd index characters from beginning is deleted: Codspeedy
Characters between 2nd index from starting and 2nd position is deleted: Cody

Thanks For Reading !!

If you have any doubt or suggestion please, comment in the comment section below. Keep Reading and Stay Tuned.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *