Determine if a string contains only alphanumeric characters (or a space)

后端 未结 4 877
遇见更好的自我
遇见更好的自我 2020-12-14 00:49

I am writing a function that determines whether a string contains only alphanumeric characters and spaces. I am effectively testing whether it matches the regular expression

4条回答
  •  抹茶落季
    2020-12-14 01:32

    And looking forward to C++0x, you'll be able to use lambda functions (you can try this out with gcc 4.5 or VS2010):

    bool string_is_valid(const std::string &str)
    {
        return find_if(str.begin(), str.end(), 
            [](char c) { return !(isalnum(c) || (c == ' ')); }) == str.end();
    }
    

提交回复
热议问题