测试字符串是否以数字开头
test if a string starts with a number
我刚开始学习C++。如果字符串以数字开头,我有哪些测试选项?我的函数is_page_number
在下面的程序中起到了作用。这是个坏主意吗?如何在此处使用正则表达式?如果代码的任何部分写得不好,我们将不胜感激。
#include <string>
#include <iostream>
#include <fstream>
using std::string;
using std::cout;
using std::ifstream;
using std::endl;
bool is_page_number(const string& aline) {
return aline[0] == '1' | aline[0] == '2' | aline[0] == '3' | aline[0] == '4' | aline[0] == '5' | aline[0] == '6' | aline[0] == '7' | aline[0] == '8' | aline[0] == '9';
}
int main() {
const string temp_filename("test_input.txt");
ifstream input(temp_filename.c_str());
string one_line;
while (getline(input,one_line)) {
if(is_page_number(one_line)) {
cout << "page number: ";
}
cout << one_line << endl;
}
}
#include <cctype>
return isdigit(aline[0]);
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 开头C++:使用 if else,n--, 数字
- 是否可以在 c++ 中创建以数字开头的类名
- C++从文件中读取不同类型的数据,直到有一个以数字开头的字符串
- C++ 在数字开头添加小数点的任意方法
- 在以 8 开头的任何数字之前选择 "a" 或 "an"
- 如何阅读可能以字符串或数字开头的行
- 测试字符串是否以数字开头
- 如何在 ofn.lpstrFilter 中用数字开始文件格式名称?它不允许在开头写 3?