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 replace the first instance of the specified pattern in the input string with a new pattern. It first performs a search for the specified pattern to be replaced and then it replaces the first instance of the given pattern with a new one.
input_string : an input string
seacrh_string : the pattern that has to be replaced in the input_string;
format_string : the pattern that replaces the search_string.
replace_first() performs an inplace replacement. There is another variation present replace_first_copy() which instead of doing an inplace replacement, returns a copy of the modified string.
replace_first() performs a case-sensistive search. There is another variation present ireplace_first() which performs a case-insensitive search. Similarly ireplace_first_copy() returns a copy of modified string instead of doing an inplace replacement.
using namespace std;
using namespace boost;
using namespace boost::algorithm;
string s1 = "Hello and Welcome to STL libraries";
string s2 = replace_first_copy(s1,"Hello","Hi");
string s3 = replace_first_copy(s1,"and","Guys");
string s4 = ireplace_first_copy(s1,"HELLO","HI");
As we can observe from the output we are first performing an inplace first where we are replacing "STL" with "Boost". Next we are performing replace on copy of the original string. In s4, we are storing the string obtained after performing replacement while searching case-insensitively.