C++ check if string is space or null

后端 未结 6 1286
死守一世寂寞
死守一世寂寞 2021-01-13 04:38

Basically I have string of whitespace \" \" or blocks of whitespace or \"\" empty in some of the lines of the files and I would

6条回答
  •  深忆病人
    2021-01-13 05:23

    Since you haven't specified an interpretation of characters > 0x7f, I'm assuming ASCII (i.e. no high characters in the string).

    #include 
    #include 
    
    // Returns false if the string contains any non-whitespace characters
    // Returns false if the string contains any non-ASCII characters
    bool is_only_ascii_whitespace( const std::string& str )
    {
        auto it = str.begin();
        do {
            if (it == str.end()) return true;
        } while (*it >= 0 && *it <= 0x7f && std::isspace(*(it++)));
                 // one of these conditions will be optimized away by the compiler,
                 // which one depends on whether char is signed or not
        return false;
    }
    

提交回复
热议问题