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 head of the input string with a new pattern. It first identifies the head by taking first N characters , where N is given by the user. It then proceeds to replace the first N characters with the new format string given by the user.
input_string : an input string
N : An integer which specifies the number of characters to be considered as head. i.e.,
format_string : the pattern that replaces the head.
(For N>=0, at most N characters are extracted. For N<0, size(Input)-|N| characters are extracted.)
replace_head() performs an inplace replacement. There is another variation present replace_head_copy() which instead of doing an inplace replacement, returns a copy of the modified string.
using namespace std;
using namespace boost::algorithm;
string str1 = "Hello and Welcome to Boost";
string str2 = "Apple Banana Mango Apple Banana Apple";
string str4 = "Hi Hi Hi Hi Hi Hi";
string str3 = replace_head_copy(str4,3,"Guys ");
As we can observe from the output we are first performing an inplace replacement where we are replacing the first 5 characters of the input string i.e., "Hello" with "Hi ". In str3, we are storing the copy of the modified string i.e., inplace replacement is not preformed.