C++ 检查字符串是否为空格或空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6325483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:56:06  来源:igfitidea点击:

C++ check if string is space or null

c++string

提问by Mark

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.

基本上,我在文件的某些行中有一串空格" "或空格块或""空白,我想知道 C++ 中是否有一个函数来检查这一点。

*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?

*注意* 作为一个附带问题,在 C++ 中,如果我想分解一个字符串并检查它的模式,我应该使用哪个库?如果我想自己编写代码,我应该知道哪些基本函数来操作字符串?有什么好的参考吗?

采纳答案by Ben Voigt

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

由于您没有指定字符的解释 > 0x7f,我假设是 ASCII(即字符串中没有高位字符)。

#include <string>
#include <cctype>

// 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;
}

回答by Jonathan Grynspan

std::string str = ...;
if (str.empty() || str == " ") {
    // It's empty or a single space.
}

回答by Tyler Davis

bool isWhitespace(std::string s){
    for(int index = 0; index < s.length(); index++){
        if(!std::isspace(s[index]))
            return false;
    }
    return true;
}

回答by jonsca

 std::string mystr = "hello";

 if(mystr == " " || mystr == "")
   //do something

In breaking a string down, std::stringstreamcan be helpful.

在打破字符串时,std::stringstream可能会有所帮助。

回答by Cheers and hth. - Alf

You don't have a nullstring "in some of the lines of the files".

“在文件的某些行中”没有空字符串。

But you can have an empty string, i.e. an empty line.

但是你可以有一个空字符串,即一个空行。

You can use e.g. std::string.length, or if you like C better, strlenfunction.

您可以使用 eg std::string.length,或者如果您更喜欢 C,则使用strlen函数。

In order to check for whitespace, the isspacefunction is handy, but note that for charcharacters the argument should be casted to unsigned char, e.g., off the cuff,

为了检查空格,该isspace函数很方便,但请注意,对于char字符,应将参数强制转换为unsigned char,例如,袖口外,

bool isSpace( char c )
{
    typedef unsigned char UChar;
    return bool( ::isspace( UChar( c ) ) );
}

Cheers & hth.,

干杯 & hth.,

回答by Dani

If you want pattern checking use regexp.

如果您想要模式检查,请使用正则表达式。