C ++检查字符串是空格还是空 [英] C++ check if string is space or null
问题描述
基本上我有一些字符串的空格或空白块或
的文件,我想知道如果有一个函数在C + +检查这个。
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?
推荐答案
因为你没有指定字符的解释> 0x7f
,我假设ASCII(即字符串中没有高字符)。
Since you haven't specified an interpretation of characters > 0x7f
, I'm assuming ASCII (i.e. no high characters in the string).
#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;
}
这篇关于C ++检查字符串是空格还是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!