Removing a Substring from a String - C++ STL

C++ examples for STL:string

Introduction

Use the find, erase, and length member functions of basic_string:

Demo Code

#include <string>
#include <iostream>

using namespace std;
int main(){// w w  w. j  av  a 2 s . co m
     std::string t = "Banana Banana Banana Banana";
     std::string s = "nana";

     std::string::size_type i = t.find(s);

     if (i != std::string::npos)
        t.erase(i, s.length());

     cout<< t;
     cout<<"\n";
     cout<< s;
}

Result


Related Tutorials