How can I check if a string has special characters in C++ effectively?

前端 未结 9 840
情话喂你
情话喂你 2021-02-08 13:15

I am trying to find if there is better way to check if the string has special characters. In my case, anything other than alphanumeric and a \'_\' is considered a special charac

9条回答
  •  不思量自难忘°
    2021-02-08 13:55

    If you want this, but don't want to go the whole hog and use regexps, and given you're test is for ASCII chars - just create a function to generate the string for find_first_not_of...

    #include 
    #include 
    
    std::string expand(const char* p)
    {
        std::string result;
        while (*p)
            if (p[1] == '-' && p[2])
            {
                for (int c = p[0]; c <= p[2]; ++c)
                    result += (char)c;
                p += 3;
            }
            else
                result += *p++;
        return result;
    }
    
    int main()
    {
        std::cout << expand("A-Za-z0-9_") << '\n';
    }
    

提交回复
热议问题