erase string cpp code example
Example 1: string erase character c++
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
std::string s = "This is an example";
std::cout << s << '\n';
s.erase(0, 5);
std::cout << s << '\n';
s.erase(std::find(s.begin(), s.end(), ' '));
std::cout << s << '\n';
s.erase(s.find(' '));
std::cout << s << '\n';
}
Example 2: c++ erase substring
using namespace std;
string BasicString = "I dont like erasing strings";
string StrToErase = " dont";
string::size_type i = BasicString.find(StrToErase);
if (i != string::npos)
{
BasicString.erase(i, StrToErase.length());
}
cout << BasicString << "\n";