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

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

     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_right" is used to remove all the trailing white-spaces in the string i.e., all the spaces present on the right side of the string as shown in the below examples:

1. Original String: "Hello nice to meet you "
Modified String: "Hello nice to meet you"

2. Original String: " You are using boost "
Modified String: " You are using boost"

header file:

     trim_right(input , loc);

    input : an input sequence
    loc :  a locale used for space classification

    The modification of the string is done in-place if trim_right() is used.    
    There is another variation present i.e., trim_right_copy() , 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.


#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; int main() { string s1 = " Welcome to Boost "; string s2 = s1; //storing another copy of string s1 in s2 string s3 = "You are using boost library "; //trimming only the string s2 trim_right(s2); //trimming s3 and storing the modified copy in newString string newString = trim_right_copy(s3); cout<<"EXAMPLE 1"<<endl; cout<<"The original string: "; cout<<s1<<"##part not in string##"<<endl; cout<<"The modified string: "; cout<<s2<<"##part not in string##"<<endl; cout<<"EXAMPLE 2"<<endl; cout<<"The original string: "; cout<<s3<<"##part not in string##"<<endl; cout<<"The modified string: "; cout<<newString<<"##part not in string##"<<endl; }



As we an observe from the above code that trim_right() takes any string as input and it removes all the trailing whitespaces. It leaves the rest of characters and symbols untouched.
Similarly, trim_right_copy()  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::find_token() 1352 0
C++ boost::algorithm::string::trim() 10928 0
C++ boost::algorithm::string::replace_first() 1922 0
C++ Program to Implement Dequeue 5075 5
C++ boost::algorithm::string::replace_nth() 722 0
C++ boost::algorithm::string::find_tail() 702 1
C++ boost::algorithm::string::find_first() 2472 1
C++ boost::algorithm::string::join() 2596 0
C++ boost::algorithm::string::erase_first() 817 1
C++ boost::algorithm::string::erase_last() 666 0
C++ boost::algorithm::string::trim_right() 1391 3
C++ boost::algorithm::string::replace_head() 583 0
C++ boost::algorithm::string::starts_with() 7618 0
C++ boost::algorithm::string::trim_right_if() 1610 1
C++ boost::algorithm::string::erase_head() 714 0
C++ boost::algorithm::string::trim_left_if() 1219 1
C++ boost::algorithm::string::replace_last() 914 0
C++ boost::algorithm::string::find_all() 2476 0
C++ Program to Implement Dequeue 626 3
C++ boost::algorithm::string::find_head() 578 0
C++ boost::algorithm::string::ends_with() 4655 0
C++ boost::algorithm::string::equals() 2618 0
C++ boost::algorithm::string::split() 3949 0
C++ boost::algorithm::string::contains() 6368 0
C++ boost::algorithm::string::trim_left() 1493 1
C++ boost::algorithm::string::erase_tail() 744 0
C++ boost::algorithm::string::all() 618 1
C++ boost::algorithm::string::replace_tail() 570 0
C++ boost::algorithm::string::trim_if() 2249 1
C++ boost::algorithm::string::erase_all() 3010 1
C++ boost::algorithm::string::lexicographical_compare() 767 0
C++ boost::algorithm::string::to_lower() 7063 1
C++ boost::algorithm::string::to_upper() 3076 1
C++ boost::algorithm::string::find_last() 1075 1
C++ boost::algorithm::replace_all() 8513 0
C++ boost::algorithm::string::erase_nth() 570 0