2013-09-03

C++ Study: Implements String trim()

This code snippet show how to trim a string (remove whitespace at the beginning and end of the string);
The implementation is inspired by Poco Library

std::string trim(std::string input) 
{
 int first = 0;
 int last = input.size() - 1;
 
 while (first <= last && isspace(input[first])) { first++; }
 while (last >= first && isspace(input[last])) { last--; }

   return input.substr(first, (last - first + 1));
}

No comments:

Post a Comment