Marc Durufle said:
I have a string like that " Vertices "
and i want to obtain "Vertices"
I would want to know if there is a simple way to put off spaces on a
string, thank you
Note: you should specify whether you intend to remove *all* spaces,
or just trim the ones at the beginning and the end of the string.
(several replies so far assume the former).
Here's a way to remove spaces at both ends of the string:
std::string trimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}
NB: - str is the original string
- sepSet is the list of space characters.
Use for example: " " or " \t" or " \t\n\r" etc.
hth