Efficient way to check if std::string has only spaces

后端 未结 12 588
别那么骄傲
别那么骄傲 2020-12-24 05:20

I was just talking with a friend about what would be the most efficient way to check if a std::string has only spaces. He needs to do this on an embedded project he is worki

相关标签:
12条回答
  • 2020-12-24 06:06

    Using strtok like that is bad style! strtok modifies the buffer it tokenizes (it replaces the delimiter chars with \0).

    Here's a non modifying version.

    const char* p = str.c_str();
    while(*p == ' ') ++p;
    return *p != 0;
    

    It can be optimized even further, if you iterate through it in machine word chunks. To be portable, you would also have to take alignment into consideration.

    0 讨论(0)
  • 2020-12-24 06:10

    If you are using CString, you can do

    CString myString = "    "; // All whitespace
    if(myString.Trim().IsEmpty())
    {
        // string is all whitespace
    }
    

    This has the benefit of trimming all newline, space and tab characters.

    0 讨论(0)
  • 2020-12-24 06:11

    To check if string has only whitespace in c++11:

    bool is_whitespace(const std::string& s) {
      return std::all_of(s.begin(), s.end(), isspace);
    }
    

    in pre-c++11:

    bool is_whitespace(const std::string& s) {
      for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
        if (!isspace(*it)) {
          return false;
        }
      }
      return true;
    }
    
    0 讨论(0)
  • 2020-12-24 06:11

    Something like:

    return std::find_if(
                str.begin(), str.end(),
                std::bind2nd( std::not_equal_to<char>(), ' ' ) )
        == str.end();
    

    If you're interested in white space, and not just the space character, then the best thing to do is to define a predicate, and use it:

    struct IsNotSpace
    {
        bool operator()( char ch ) const
        {
            return ! ::is_space( static_cast<unsigned char>( ch ) );
        }
    };
    

    If you're doing any text processing at all, a collection of such simple predicates will be invaluable (and they're easy to generate automatically from the list of functions in <ctype.h>).

    0 讨论(0)
  • 2020-12-24 06:15

    it's highly unlikely you'll beat a compiler optimized naive algorithm for this, e.g.

    string::iterator it(str.begin()), end(str.end())    
    for(; it != end && *it == ' '; ++it);
    return it == end;
    

    EDIT: Actually - there is a quicker way (depending on size of string and memory available)..

    std::string ns(str.size(), ' '); 
    return ns == str;
    

    EDIT: actually above is not quick.. it's daft... stick with the naive implementation, the optimizer will be all over that...

    EDIT AGAIN: dammit, I guess it's better to look at the functions in std::string

    return str.find_first_not_of(' ') == string::npos;
    
    0 讨论(0)
  • 2020-12-24 06:18
    if(str.find_first_not_of(' ') != std::string::npos)
    {
        // There's a non-space.
    }
    
    0 讨论(0)
提交回复
热议问题