C++ boost::algorithm::replace_all()














































C++ boost::algorithm::replace_all()



replace_all():

     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 all the instances of the specified pattern in the input string with a new pattern. It first performs a search for the instances of the specified pattern to be replaced and then it replaces it with the specifined format string.

header file:

boost/algorithm/string.hpp

syntax:

     replace_all(input_string,search_string,format_string);

parameters:

    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.

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.

replace_all() performs an inplace replacement. There is another variation present replace_all_copy() which instead of doing an inplace replacement, returns a copy of the modified string.

replace_all() performs a case-sensistive search. There is another variation present ireplace_all() which performs a case-insensitive search. Similarly ireplace_all_copy() returns a copy of modified string instead of doing an inplace replacement.

SAMPLE CODE:

#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;
replace_all(str1,
"Hello ","Hi ");//replacing "Hello" inplace with "Hi"
cout<<str1<<endl<<endl;

string str2 = "Apple Banana Mango Apple Banana Apple";
cout<<str2<<endl;
ireplace_all(str2,
"apple","Orange");//replacing "Apple" inplace with "Orange"
cout<<str2<<endl<<endl;

string str4 = "Hi Hi Hi Hi Hi Hi";
cout<<str4<<endl;
string str3 = replace_all_copy(str4,"Hi ","Guys ");//removing "Hi " by returning a copy
cout<<str3<<endl;

}

OUTPUT:

EXPLANATION:

As we can observe from the output we are first performing an inplace replacement where we are replacing the all the occurences of "Hello " with "Hi" . Next we are performing replacement on the original string while performing the search case-insensitively. In str3, we are storing the string obtained after performing replacement while searching case-sensitively. Her in the last case we can observe that the last instance of "Hi" is not getting replaced as the search string we have provided is "Hi "(with a trailing whitespace).


More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 691 0
C++ boost::algorithm::string::split() 833 0
C++ boost::algorithm::string::find_all() 747 0
C++ boost::algorithm::string::erase_tail() 270 0
C++ boost::algorithm::string::replace_tail() 229 0
C++ boost::algorithm::string::erase_head() 303 0
C++ boost::algorithm::string::replace_head() 232 0
C++ boost::algorithm::string::erase_all() 1049 1
C++ boost::algorithm::replace_all() 2455 0
C++ boost::algorithm::string::erase_nth() 232 0
C++ boost::algorithm::string::replace_nth() 238 0
C++ boost::algorithm::string::replace_last() 279 0
C++ boost::algorithm::string::erase_last() 271 0
C++ boost::algorithm::string::erase_first() 238 1
C++ boost::algorithm::string::replace_first() 492 0
C++ boost::algorithm::string::find_token() 413 0
C++ boost::algorithm::string::find_tail() 217 1
C++ boost::algorithm::string::find_head() 231 0
C++ boost::algorithm::string::find_last() 341 1
C++ boost::algorithm::string::find_first() 848 1
C++ boost::algorithm::string::all() 256 1
C++ boost::algorithm::string::lexicographical_compare() 234 0
C++ boost::algorithm::string::equals() 479 0
C++ boost::algorithm::string::contains() 2117 0
C++ boost::algorithm::string::ends_with() 1489 0
C++ boost::algorithm::string::starts_with() 2387 0
C++ boost::algorithm::string::trim_if() 797 1
C++ boost::algorithm::string::trim() 4510 0
C++ boost::algorithm::string::trim_right_if() 581 1
C++ boost::algorithm::string::trim_left_if() 447 1
C++ boost::algorithm::string::trim_right() 348 3
C++ boost::algorithm::string::trim_left() 419 1
C++ boost::algorithm::string::to_lower() 1076 1
C++ boost::algorithm::string::to_upper() 404 1
C++ Program to Implement Dequeue 1619 5
C++ Program to Implement Dequeue 262 3

Comments