Trim Strings – Boost String Algorithm in C++

This article provides a detailed explanation of ‘How to Trim Strings in C++ using the Boost String Algorithm’. Above all, ‘in just a single line of code’. Before, you read the article. Feel free to visit this to get an idea regarding strings in C++.

Removing extra-space from user-typed input is a very important task. For instance, take the case of a user typing their first name in a form. Hence, accomplishing this task is of extreme importance. For this, C++ provides a special library “boost/algorithm/string.hpp” containing in it some functions which are remarkably useful when it comes to string-related implementations.

 

The “boost/algorithm/string.hpp” header has in it, the following useful functions:

  • trim( arguments ), trim_copy( arguments )
  • to_lower( arguments ), to_upper( arguments )
  • replace_first( arguments ), replace_nth( arguments )
  • erase_first( arguments ), erase_nth( arguments )

And many more. However, this article will solely focus on the functions used for trimming strings. Now, have a look at the code. After that, scroll down for a detailed explanation. You will get your doubts cleared there.

 

C++ CODE :

//Programme to illustrate trim() and trim_copy() function

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

using namespace std; 
using namespace boost::algorithm; 

int main() 
{ 
    string sOrg = " Hi there, this is an illustration of trim()      "; 
    string sCopy = sOrg; 
    
    cout<<"Before Trimming : "<<endl; 
    
    cout<<"Original String        : String begins here ->";
    cout<< sOrg <<"<- String ends here"<<endl; 
    
    trim(sOrg); 
    string trimmedCopy = trim_copy(sCopy); 
    
    cout<<"\nAfter Trimming  : "<<endl;
    
    cout<<"Original String        : String begins here ->";
    cout<< sOrg <<"<- String ends here"<<endl;
    
    cout<<"Copy of Trimmed String : String begins here ->";
    cout<<trimmedCopy<<"<- String ends here"<<endl; 
    
    return 0; 
        
}

OUTPUT :

Before Trimming : 
Original String        : String begins here -> Hi there, this is an illustration of trim()      <- String ends here

After Trimming  : 
Original String        : String begins here ->Hi there, this is an illustration of trim()<- String ends here
Copy of Trimmed String : String begins here ->Hi there, this is an illustration of trim()<- String ends here

 

You might have the following questions in your mind:

Trim Strings - Boost String Algorithm in C++

 

  1. What is the “boost/algorithm/string.hpp” header?
    • This header contains in it the above-said functions used for string related implementations.
    • Make sure you use the namespace boost::algorithm alongside.
  2. What is a ‘namespace’?
    • A namespace is a region the allows identifiers to be declared and provided scope.
    • These are especially useful when your code uses multiple libraries.
  3. How to use the trim( arguments ) method?
    • Detailed explanation in the following.

 

Using the trim( arguments  ) and trim_copy( arguments ) functions

 

Now, coming to the code. Here, we initially initialise a string variable ‘sOrg’, which holds a string with a whitespace at either end. We make a copy of the string to ‘sCopy’, to make further implementations on the same initial string. Now, we call the trim( arguments ) function, which takes a single argument and trims white-space from either end of the string, changing the string. Use trim( arguments ) when you want to change the original string.

Syntax : trim( string_to_be_trimmed );

 

There is a variant of this function, which is the trim_copy( arguments ). This function makes a copy after trimming the string given as the argument and returns the copy. This leaves the original string intact. Use trim_copy( ) when you don’t want to make changes to the original string.

Syntax : string_name = trim_copy( string_to_be_trimmed );

 

Now, have a look at the code yet again. Any doubts remaining should be clarified.

 

Leave a Reply