64,648
社区成员
发帖
与我相关
我的任务
分享
1. 使用trim函数
#include <boost/algorithm/string.hpp>
bool isBlank(const std::string &str){
std::string tmp = str;
boost::algorithm::trim(tmp);
return tmp.length() == 0;
}
2. 使用boost正则表达式
#include <boost/regex.hpp>
bool isBlank(const std::string &str){
const boost::regex e("\\s*");
return boost::regex_match(str, e);
}
编译的时候需要链接boost_regex库, 即添加链接参数-l boost_regex
#include <iostream>
#include <string>
bool IsBlankLine(const std::string& str)
{
int len = str.length();
int pos = 0;
while (pos < len) {
if (str[pos] == ' ')
pos++;
// 这里仅是拿ANSI举例,全角空格是A1 A1
// 如果是utf文本的话,全角空格是E3 80 80 看文件开头是否EF BB BF,
// 也可能是没标志,不过utf和ansi很容易区分出来。
// 如果是unicode ,全角空格是00 30 看文件开头是否是FF FE
else if ( str[pos] == '\xA1' && str[pos + 1] == '\xA1')
pos += 2;
else
return false;
}
return true;
}
int main()
{
using namespace std;
string str = " ";
if (IsBlankLine(str))
cout << "Is blank line" << endl;
}