Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

C++ String Trim Blanks

DZone's Guide to

C++ String Trim Blanks

·
Free Resource
// description of your code here


#include
  
   
#include
   
    
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) ;
}  

   
  
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}