C++ boost::algorithm::string::erase_nth()

C++ boost::algorithm::string::erase_nth()


     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.

header file:





    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.


#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; int main() { string str1 = "Hello and Welcome Hello and Welcome"; cout<<str1<<endl; erase_nth(str1,"Hello ",0);//removing 0 instance of "Hello" cout<<str1<<endl<<endl; string str2 = "Apple Banana Mango Apple Banana Apple"; cout<<str2<<endl; ierase_nth(str2,"apple",1);//removing 1 instance of "Apple" inplace cout<<str2<<endl<<endl; string str4 = "Hi Hi Hi Hi Hi Hi"; cout<<str4<<endl; string str3 = erase_nth_copy(str4,"Hi ",3);//removing 3 instance of "Hi " by returning a copy cout<<str3<<endl; }



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.

More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 577 0
C++ boost::algorithm::string::split() 607 0
C++ boost::algorithm::string::find_all() 647 0
C++ boost::algorithm::string::erase_tail() 249 0
C++ boost::algorithm::string::replace_tail() 212 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 210 0
C++ boost::algorithm::string::erase_all() 898 1
C++ boost::algorithm::replace_all() 2050 0
C++ boost::algorithm::string::erase_nth() 216 0
C++ boost::algorithm::string::replace_nth() 221 0
C++ boost::algorithm::string::replace_last() 247 0
C++ boost::algorithm::string::erase_last() 244 0
C++ boost::algorithm::string::erase_first() 215 1
C++ boost::algorithm::string::replace_first() 433 0
C++ boost::algorithm::string::find_token() 351 0
C++ boost::algorithm::string::find_tail() 200 1
C++ boost::algorithm::string::find_head() 213 0
C++ boost::algorithm::string::find_last() 301 1
C++ boost::algorithm::string::find_first() 749 1
C++ boost::algorithm::string::all() 225 1
C++ boost::algorithm::string::lexicographical_compare() 214 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1826 0
C++ boost::algorithm::string::ends_with() 1283 0
C++ boost::algorithm::string::starts_with() 2059 0
C++ boost::algorithm::string::trim_if() 712 1
C++ boost::algorithm::string::trim() 3797 0
C++ boost::algorithm::string::trim_right_if() 522 1
C++ boost::algorithm::string::trim_left_if() 385 1
C++ boost::algorithm::string::trim_right() 282 3
C++ boost::algorithm::string::trim_left() 364 1
C++ boost::algorithm::string::to_lower() 737 1
C++ boost::algorithm::string::to_upper() 298 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3