Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ check if string is space or null

Tags:

c++

string

Basically I have string of whitespace " " or blocks of whitespace or "" empty in some of the lines of the files and I would like to know if there is a function in C++ that checks this.

*note:* As a side question, in C++ if I want to break a string down and check it for a pattern which library should I use? If I want to code it myself which basic functions should I know to manipulate string? Are there any good references?

like image 976
Mark Avatar asked Jun 12 '11 23:06

Mark


2 Answers

bool isWhitespace(std::string s){
    for(int index = 0; index < s.length(); index++){
        if(!std::isspace(s[index]))
            return false;
    }
    return true;
}
like image 189
Tyler Davis Avatar answered Sep 19 '22 17:09

Tyler Davis


std::string str = ...;
if (str.empty() || str == " ") {
    // It's empty or a single space.
}
like image 29
Jonathan Grynspan Avatar answered Sep 18 '22 17:09

Jonathan Grynspan