判斷是否爲純數字字符串

Qt判斷是否爲純數字字符串 

bool CTest::isDigitStr(const QString& str)
{
    QByteArray ba = str.toLocal8Bit();  //分開寫是爲了防止亂碼
    char* s = ba.data();
    while (*s && *s >= '0' && *s <= '9')
    {
        s++;
    }
    return !bool(*s);
}

 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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章