std::string::erase in C++

The function erases a part of the string content, shortening the length of the string. The characters affected depend on the member function version used:
Return value : erase() returns *this.

  1. Syntax 1: Erases all characters in a string
    string& string ::erase ()
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // erase() function
      
    #include <iostream>
    #include <string>
    using namespace std;
      
    // Function to demo erase()
    void eraseDemo(string str)
    {
        // Deletes all characters
        str.erase();
      
        cout << "After erase() : ";
        cout << str;
    }
      
    // Driver code
    int main()
    {
        string str("Hello World!");
      
        cout << "Before erase() : ";
        cout << str << endl;
        eraseDemo(str);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Before erase() : Hello World!
    After erase() : 
    
  2. Syntax 2: Erases all characters after position ‘pos’
    string& string ::erase (size_type pos)
    - Throw out_of_range if idx > size().
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate working of
    // erase(idx)
      
    #include <iostream>
    #include <string>
    using namespace std;
      
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes all characters except first one
        str.erase(1);
      
        cout << "After erase(idx) : ";
        cout << str;
    }
      
    // Driver code
    int main()
    {
        string str("Hello World!");
      
        cout << "Before erase(idx) : ";
        cout << str << endl;
        eraseDemo(str);
      
        return 0;
    }

    chevron_right

    
    

    Output:



    Before erase(idx) : Hello World!
    After erase(idx) : H
    
  3. Syntax 3: Erases at most, len characters of *this, starting at index idx.
    string& string ::erase (size_type idx, size_type len )
    - If len is missing, all remaining characters are removed.
    - Throw out_of_range if idx > size().
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // erase(size_type idx, size_type len )
    #include <iostream>
    #include <string>
    using namespace std;
      
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes 4 characters from index number 1
        str.erase(1, 4);
      
        cout << "After erase : ";
        cout << str;
    }
      
    // Driver code
    int main()
    {
        string str("Hello World!");
      
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Before erase : Hello World!
    After erase : H World!
    
  4. Syntax 4: Erase the single character at iterator position pos.
    string& string ::erase (iterator pos)
    - Return the first character after the last character removed
    - If no such character is remaining then, returns 
      string::end() i.e. position after the last character.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // erase(iterator pos)
      
    #include <iostream>
    #include <string>
    using namespace std;
      
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes character at position 4
        str.erase(str.begin() + 4);
      
        cout << "After erase : ";
        cout << str;
    }
      
    // Driver code
    int main()
    {
        string str("Hello World!");
      
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Before erase : Hello World!
    After erase : Hell World!
    
  5. Syntax 5: Erase the single character at iterator position pos.
    string& string ::erase (iterator beg, iterator end )
    - Erases all characters of the range [ beg, end)
    - Returns end i.e. the first character after the
      last character removed.
    - If no such character is remaining then, returns 
      string::end() i.e. position after the last character
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // erase(iterator pos, iterator end)
      
    #include <iostream>
    #include <string>
    using namespace std;
      
    // Function to demo erase
    void eraseDemo(string str)
    {
        // Deletes all characters between 0th index and
        // str.end() - 6
        str.erase(str.begin() + 0, str.end() - 6);
      
        cout << "After erase : ";
        cout << str;
    }
      
    // Driver code
    int main()
    {
        string str("Hello World!");
      
        cout << "Before erase : ";
        cout << str << endl;
        eraseDemo(str);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Before erase : Hello World!
    After erase : World!
    

Related Article : std::string::clear
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.