You can use the erase-remove idiom with the predicate std::isalnumas shown below:
std::string StripNonAlphaNum(std::string token) {
token.erase(std::remove_if(token.begin(), token.end(),
[](char const &c){ return !std::isalnum(c); }), token.end());
return token;
}
Live demo
The same can be implemented for std::wstring:
std::wstring StripNonAlphaNum(std::wstring token) {
token.erase(std::remove_if(token.begin(), token.end(),
[](char const &c){ return !std::iswalnum(c); }), token.end());
return token;
}
Live demo
source
share