- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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<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) $
- Related Articles
- What is the best way to read an entire file into a std::string in C++?
- What's the best way to ensure a happy marriage?
- What's the best way to detect a 'touch screen' device using JavaScript?
- What's the best way to open new browser window using JavaScript?
- The correct way to trim a string in Java.
- What does 'using namespace std' mean in C++?
- What's the canonical way to check for type in python?
- What's the best cheap hosting provider in India?
- The best way to hide a string in binary code in C++?
- What's the simplest way to print a Java array?
- What's the Kotlin equivalent of Java's String[]?
- What's the best city in India to watch a movie?
- Easy way to remember Strassen's Matrix Equation in C++
- What is the best way to convert a number to a string in JavaScript?
- What is the easiest way to initialize a std::vector with hardcoded elements in C++?

Advertisements