Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nishtha Thakur

Here we will see how to trim the strings in C++. The trimming string means removing whitespaces from left and right part of the string.

To trim the C++ string, we will use the boost string library. In that library, there are two different methods called trim_left() and trim_right(). To trim string completely, we can use both of them.

Example

#include<iostream>
#include<boost/algorithm/string.hpp>
using namespace std;
main(){
   string myStr = " This is a string ";
   cout << "The string is: (" << myStr << ")" << endl;
   //trim the string
   boost::trim_right(myStr);
   cout << "The string is: (" << myStr << ")" << endl;
   boost::trim_left(myStr);
   cout << "The string is: (" << myStr << ")" << endl;
}

Output

$ g++ test.cpp
$ ./a.out
The string is: (       This is a string         )
The string is: (       This is a string)
The string is: (This is a string)
$

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.