Removes specified characters from the string.
position
.[first, last)
.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 |
*this
end()
if no such character existslast
pointed to before the erase, or end()
if no such character existsIn any case, if an exception is thrown for any reason, this function has no effect (strong exception guarantee). (since C++11).
#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:
clears the contents (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/erase