std::basic_string::erase
From cppreference.com
< cpp | string | basic string
basic_string& erase( size_type index = 0, size_type count = npos );
|
(1) | |
iterator erase( iterator position );
iterator erase( const_iterator position ); |
(2) | (until C++11) (since C++11) |
iterator erase( iterator first, iterator last );
iterator erase( const_iterator first, const_iterator last ); |
(3) | (until C++11) (since C++11) |
Removes specified characters from the string.
1) Removes
count
characters starting at index
.
2) Removes the character at
position
.
3) Removes the character in the range
[first; last)
.
Contents |
[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-3) iterator following the last removed character.
[edit] Exceptions
2-3) (none)
[edit] Example
#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
clears the contents (public member function) |