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