Thursday, December 11, 2008

Chomp whitespaces off an ascii string

Here is another version of the chomp that chomps away leading and trailing spaces from a string


std::string& chomp(std::string& str)
{
int fidx = 0;
for( ; fidx < str.size() > ++fidx )
{
if( str[fidx] != ' ')
{
break;
}
}

int ridx = str.size() - 1;
for(; ridx > fidx; --ridx)
{
if( str[ridx] != ' ')
{
break;
}
}

str.substr(fidx, ridx - fidx + 1 ).swap(str);
return str;
}

No comments: