C++判断是否为纯数字字符串

#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <sstream>
using namespace std;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    QString str = "lfr_bs_r_11";
    QStringList tmp = str.split("_");
    QString no = tmp.last();
    qDebug() << "no:" << no << str.toInt() << no.toInt();

    QString str1 = "dqf4r34fea";
    QString str2 = "123467";

    if(isNum(str1.toStdString())){
        qDebug() << "str1 is a num!";
    }else{
        qDebug() << "str1 is not a num!";
    }

    if(isNum(str2.toStdString())){
        qDebug() << "str2 is a num!";
    }else{
        qDebug() << "str2 is not a num!";
    }
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::isNum(string str)
{
    stringstream sin(str);
    double d;
    char c;
    if(!(sin >> d))
        return false;
    if (sin >> c)
        return false;
    return true;
}

猜你喜欢

转载自blog.csdn.net/graceland525/article/details/78110881
今日推荐