Sunday, January 1, 2012

Trim function for std::string

This is a simple and efficient function to trim a string class in C++

void trim(string& astring, const char t){
   string::iterator it;
   
   for (it=astring.begin() ; it < astring.end(); it++){
      if(*it==t){
           astring.erase(it);
       };
   }
}

usage example

trim (number, ' ');

I think it is quite clear.

No comments:

Post a Comment