This function is included in the "boost/algorithm/string" library. This library contains some brilliant methods which help in accomplishing string manipulations that are lacking in STL library.
This function is used to remove the n'th instance of the specified pattern in the input string. It first performs a search for the n'th occurence of the specified pattern and then it erases it from the input string.
input_string : an input string
seacrh_string : the pattern that has to be replaced in the input_string;
N : An integer which specifies the occurence of the pattern to be removed. i.e., An index of the match to be removed.
The index is 0-based index i.e., the first occurence is considered 0. For negative values of N, matches are counted from the end of the string i.e., the last occurence is -1 and so on.
erase_nth() performs an inplace removal. There is another variation present erase_nth_copy() which instead of doing an inplace removal, returns a copy of the modified string.
erase_nth() performs a case-sensistive search. There is another variation present ierase_nth() which performs a case-insensitive search. Similarly ierase_nth_copy() returns a copy of modified string instead of doing an inplace removal.
using namespace std;
using namespace boost::algorithm;
string str1 = "Hello and Welcome Hello and Welcome";
string str2 = "Apple Banana Mango Apple Banana Apple";
string str4 = "Hi Hi Hi Hi Hi Hi";
string str3 = erase_nth_copy(str4,"Hi ",3);
As we can observe from the output we are first performing an inplace removal where we are removing the 0th occurence of "Hello ". Next we are performing removal on the original string while performing the search case-insensitively. In str3, we are storing the string obtained after performing removal while searching case-sensitively.