c++ - 将字符串解析为数字或数字和百分号

标签 c++ string parsing

在 C++ 中,最好没有 Boost 库,如何确保 std::string str 包含数字或数字后跟 '%' 符号?如果不属于这两种情况,则应发出错误。

最佳答案

#include <iostream>
#include <string>
#include <algorithm>
#include <ctype.h>
bool is_a_bad_char(char c) {
  return !(isdigit(c) || (c=='%'));
}

int main() {
  std::string str = "123123%4141219";
  if (std::find_if(str.begin(), str.end(), is_a_bad_char) != str.end()) {
    std::cout << "error" << std::endl;
    return 1;
  }
  return 0;
}

关于c++ - 将字符串解析为数字或数字和百分号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666252/

相关文章:

parsing - 为什么 ANTLR 不解析整个输入?

c++ - 在 C++ 中实现逐行文件解析器时遇到问题

c++ - WinRT 上的 UuidToString

mysql - 选择包含子字符串的 MySQL 字段

python - 为什么我不能在 map() 中使用字符串函数?

java - KML的字符串需要转换为java对象

c++ - 如何将 "add"用户自定义格式标志转为 basic_ostream?

c++ - SFML 在实现绘制函数时从派生类转换不明确

c++ - 从多重继承/菱形继承(钻石问题)

c# - 用于 C# 的 Lex/Yacc?