How to trim strings in C++ using Boost String Algorithm Library

In the previous article, we have discussed about Program for Transpose a Matrix in Python & C++ Programming. Let us learn how to trim strings in C++ Program.

We are going to see how we can trim strings by using C++ Boost string library. The BOOST library provides a lot of different ways to trim strings.

First we will see how we can trim the string of white spaces from left side, right side and also from both the sides.

Trim String from Both sides :

Below code is the implementation for this.

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

void trimBothSides()
{
    std::string message = "   Hey!Ssup?   ";
    boost::algorithm::trim(message);
    std::cout << "[" << message << "]" << std::endl;
}

int main()
{
    trimBothSides();
    return 0;
}
Output :
[Hey!Ssup?]

Here the string containing the whitespaces at both the ends got trimmed containing only our text and no whitespace.

Trim From Left side :

Below code is the implementation for this.

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

void trimLeftSide()
{
    std::string message = "   Hey!Ssup?   ";
    boost::algorithm::trim_left(message);
    std::cout << "[" << message << "]" << std::endl;
}

int main()
{
    trimLeftSide();
    return 0;
}
Output :
[Hey!Ssup?   ]

As we were supposed to do, the whitespaces from the left were removed while the ones on the right were kept.

Trim From Right side :

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

void trimRightSide()
{
    std::string message = "   Hey!Ssup?   ";
    boost::algorithm::trim_right(message);
    std::cout << "[" << message << "]" << std::endl;
}

int main()
{
    trimRightSide();
    return 0;
}
Output :
[   Hey!Ssup?]

As we were supposed to do, the whitespaces from the left were kept while the ones on the right were removed.

Trim the copy of string :

 In the above examples we were modifying the original value itself. However, situations may arise where we might require both the original and modified values. For that we are going to use

  • trim_copy( )
  • trim_left_copy( )
  • trim_right_copy( )
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

void trimCopy()
{
    std::string message = "   Hey!Ssup?   ";
    modStr = boost::algorithm::trim_copy(message);
    std::cout << "Using trim_copy[" << modStr << "]" << std::endl;

    modStr = boost::algorithm::trim_right_copy(message);
    std::cout << "Using trim_right_copy[" << modStr << "]" << std::endl;

    modStr = boost::algorithm::trim_left_copy(message);
    std::cout << "Using trim_left_copy[" << modStr << "]" << std::endl;
}

int main()
{
    trimCopy();
    return 0;
}
Output :
Using trim_copy[Hey!Ssup?]
Using trim_right_copy[   Hey!Ssup?]
Using trim_left_copy[Hey!Ssup?   ]

Condition based trimming of Strings

If we want to trim some other characters except whitespaces, we can do so by using condition based functions.

  • trim_if( )
  • trim_left_if( )
  • trim_right_if( )
  • trim_copy_if( )
  • trim_left_copy_if( )
  • trim_right_copy_if( )

Let’s use the following string-

;;Hey;;!;;Ssup;;?;;

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>

bool isColon(char c)
{
    return c == ';';
}

void trimIfCopy()
{
    std::string message = ";;Hey;;!;;Ssup;;?;;";
    boost::algorithm::trim_if(message, &isColon);
    std::cout << "Using trim_if[" << message << "]" << std::endl;

    std::string message = ";;Hey;;!;;Ssup;;?;;";
    boost::algorithm::trim_right_if(message, &isColon);
    std::cout << "Using trim_right_if[" << message << "]" << std::endl;

    std::string message = ";;Hey;;!;;Ssup;;?;;";
    boost::algorithm::trim_left_if(message, &isColon);
    std::cout << "Using trim_left_if[" << message << "]" << std::endl;

    std::string message = ";;Hey;;!;;Ssup;;?;;";
    modStr = boost::algorithm::trim_copy_if(message, &isColon);
    std::cout << "Using trim_copy_if[" << modStr << "]" << std::endl;

    modStr = boost::algorithm::trim_right_copy_if(message, &isColon);
    std::cout << "Using trim_right_copy_if[" << modStr << "]" << std::endl;

    modStr = boost::algorithm::trim_left_copy_if(message, &isColon);
    std::cout << "Using trim_left_copy_if[" << modStr << "]" << std::endl;

}

int main()
{
    trimIfCopy();
    return 0;
}
Output :
Using trim_if[Hey!Ssup?]
Using trim_right_if[;;Hey;;!;;Ssup;;?]
Using trim_left_if[Hey;;!;;Ssup;;?;;]
Using trim_copy_if[Hey!Ssup?]
Using trim_right_copy_if[;;Hey;;!;;Ssup;;?]
Using trim_left_copy_if[Hey;;!;;Ssup;;?;;]