Tuesday, December 9, 2008

Chomp whitespaces off an ascii string

Few days ago while programming some intresting stuff I was in dire need of some function that would chomp off white spaces in the string. Could not find one in the standard library so I have put up a small code that does the same:

static std::string& chomp(std::string& classname)
{
std::string chompedname;
chompedname.reserve(classname.size());
for( int i = 0 ; i < classname.size(); ++i )
{
if( classname[i] != ' ' )
{
chompedname.push_back(classname[i]);
}
}
classname.swap(chompedname);
return classname;
}

No comments: