What's the best way to trim std::string in C++?

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
#include
using namespace std;
main(){
   string myStr = " This is a string ";
   cout 

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)
$
Updated on: 2019-07-30T22:30:26+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements