I18n - a friendly way to remove non-alphanumeric characters

I want to strip out any non-letter characters in a string. At the moment I am using the following function, but this will certainly not work for many languages. Is there any way to do this using i18n?

static string StripNonAlphaNum(const string& token) {
    string s = token;
     // strip away non-alphanumeric parts using a RE expression
    RE2::GlobalReplace(&s, "[^A-Za-z0-9]", "");
    return s;
}
+4
source share
1 answer

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

+4
source

Source: https://habr.com/ru/post/1625197/


All Articles