DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C++ String Trim Blanks

09.23.2009
| 8610 views |
  • submit to reddit
        // description of your code here

#include<string>
#include<iostream>
using namespace std;

#define SPACES " \t\r\n"

inline string trim_right (const string & s, const string & t = SPACES)
{
    string d (s);
    string::size_type i (d.find_last_not_of (t));
    if (i == string::npos)
        return "";
    else
        return d.erase (d.find_last_not_of (t) + 1) ;
}  

inline string trim_left (const string & s, const string & t = SPACES)
{
    string d (s);
    return d.erase (0, s.find_first_not_of (t)) ;
}  

inline string trim (const string & s, const string & t = SPACES)
{
    string d (s);
    return trim_left (trim_right (d, t), t) ;
}