erase_last():
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 erase the last instance of the specified pattern in the input string. It first performs a search for the specified pattern to find its last occurence and then removes it.
header file:
boost/algorithm/string.hpp
syntax:
erase_last(input_string,search_string);
parameters:
input_string : an input string
seacrh_string : the pattern that has to be erased from the input_string;
erase_last() performs an inplace removal. There is another variation present erase_last_copy() which instead of doing an inplace removal, returns a copy of the modified string.
erase_last() performs a case-sensistive search. There is another variation present ierase_last() which performs a case-insensitive search. Similarly ireplace_last_copy() returns a copy of modified string instead of doing an inplace removal.
SAMPLE CODE:
using namespace std; using namespace boost::algorithm; int main() { string str1 = "Hello and Welcome Hello and Welcome"; cout<<str1<<endl; erase_last(str1,"Hello ");//removing "Hello" inplace cout<<str1<<endl<<endl; string str2 = "Apple Banana Mango Banana Apple"; cout<<str2<<endl; ierase_last(str2,"apple");//removing "Apple" inplace cout<<str2<<endl<<endl; string str3 = erase_last_copy(str1,"and ");//removing "and" by returning a copy cout<<str3<<endl;}OUTPUT:
Comments