##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::erase

From cppreference.com
< cpp‎ | string‎ | basic string

basic_string& erase( size_type index = 0, size_type count = npos ); | (1) |
| (2) |
iterator erase( iterator position ); |  | (until C++11)
iterator erase( const_iterator position ); |  | (since C++11)
| (3) |
iterator erase( iterator first, iterator last ); |  | (until C++11)
iterator erase( const_iterator first, const_iterator last ); |  | (since C++11)

Removes specified characters from the string.

1) Removes min(`count`, size()`- index`) characters starting at `index`.
2) Removes the character at `position`.
3) Removes the characters in the range `[first, last)`.

### [edit] Parameters

index | - | first character to remove
count | - | number of characters to remove
position | - | iterator to the character to remove
first, last | - | range of the characters to remove

### [edit] Return value

1) *this
2) iterator pointing to the character immediately following the character erased, or `end()` if no such character exists
3) iterator pointing to the character `last` pointed to before the erase, or `end()` if no such character exists

### [edit] Exceptions

1) std::out_of_range if index > size().
2-3) (none)

In any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee). (since C++11)

### [edit] Example

Run this code

    #include <iostream>
    #include <algorithm>
    #include <string>
    int main ()
    {
        std::string s = "This is an example";
        std::cout << s << '\n';

        s.erase(0, 5); // Erase "This "
        std::cout << s << '\n';

        s.erase(std::find(s.begin(), s.end(), ' ')); // Erase ' '
        std::cout << s << '\n';

        s.erase(s.find(' ')); // Trim from ' ' to the end of the string
        std::cout << s << '\n';
    }

Output:

    This is an example
    is an example
    isan example
    isan

### [edit] See also

clear | clears the contents (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/erase&oldid=87216"