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














































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



trim_if():

     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 "trim_if" is used to remove all the leading and trailing characters(specified by the boolean function to be written by the user) in the string i.e., it does condition based trimming on the start and end of the string. Check out the examples below:


Examples:

1. Removing "#" symbols:

Original String: "####Hello nice to meet you###"

Modified String: "Hello nice to meet you"


2. Removing "@" and "$" symbols:

Original String: "@@@@You are using boost$$"

Modified String: "You are using boost"


header file:

boost/algorithm/string.hpp


syntax:

     trim_if(input ,predicate);


parameters:

    input : an input sequence

    predicate :  a function which determines what character(s) to trim.


The user has to write a function explicitly (that returns a boolean value) that specifies the character that the user wishes to trim.


    The modification of the string is done in-place if trim_if() is used.    

    There is another variation present i.e., trim_copy_if() , which takes the same parameters as input, but instead of  modifying the string in-place it returns a copy of the modified string while keeping the original string as it is.


SAMPLE CODE:

#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; //The function which returns TRUE if the character is '#' or '@' bool ischar(char c) { return c=='#' || c=='@'; } int main() { string s1 = "##Welcome to boost@@"; string s2 = "You are using boost##"; string s3 = "@@its very interesting"; //trimming all the '#' symbols from the left of string trim_if(s1,ischar); trim_if(s2,ischar); //returning the copy of trimmed string string newString = trim_copy_if(s3,ischar); cout<<"MODIFIED STRINGS"<<endl; cout<<s1<<endl; cout<<s2<<endl<<endl; cout<<"Original String: "<<s3<<endl; cout<<"Modified String: "<<newString<<endl; }

OUTPUT:


EXPLANATION:
As we can observe from the above code that trim_if() takes any string as input and it removes all the leading and trailing "#" or "@" symbols . It leaves the rest of characters and symbols untouched.
Similarly, trim_copy_if()  instead of modifying the original string i.e., "s3", it returns the modified string to "newString".

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() 201 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() 713 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

Comments