How to remove all instances of the pattern from a string?
string str = "red tuna, blue tuna, black tuna, one tuna";
string pattern = "tuna";
                Removes all instances of the pattern from a string,
#include <string>
#include <iostream>
using namespace std;
void removeSubstrs(string& s, string& p) { 
  string::size_type n = p.length();
  for (string::size_type i = s.find(p);
      i != string::npos;
      i = s.find(p))
      s.erase(i, n);
}
int main() {
  string str = "red tuna, blue tuna, black tuna, one tuna";
  string pattern = "tuna";
  removeSubstrs(str, pattern);
  cout << str << endl;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With