测试字符串是否以数字开头

test if a string starts with a number

本文关键字:数字 开头 是否 字符串 测试      更新时间:2023-10-16

我刚开始学习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]);