qt中如何判断字符串是否为数字,整数,浮点数?

在 Qt 中,可以使用多种方法来判断字符串是否为数字、整数或浮点数。Qt 提供了一些方便的字符串和数值处理函数,可以帮助你实现这些判断。以下是几种常见的方法:

1. 使用 QRegularExpression

Qt 提供了 QRegularExpression 类,可以用来匹配字符串模式。

判断是否为数字(整数或浮点数)
cpp 复制代码
#include <QRegularExpression>
#include <QString>
#include <QDebug>

bool isNumber(const QString &str) {
    QRegularExpression re("^-?\\d+(\\.\\d+)?$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456.78";
    QString testStr3 = "abc";

    qDebug() << "Is testStr1 a number?" << isNumber(testStr1);  // true
    qDebug() << "Is testStr2 a number?" << isNumber(testStr2);  // true
    qDebug() << "Is testStr3 a number?" << isNumber(testStr3);  // false

    return 0;
}
判断是否为整数
cpp 复制代码
bool isInteger(const QString &str) {
    QRegularExpression re("^-?\\d+$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456";
    QString testStr3 = "456.78";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 an integer?" << isInteger(testStr1);  // true
    qDebug() << "Is testStr2 an integer?" << isInteger(testStr2);  // true
    qDebug() << "Is testStr3 an integer?" << isInteger(testStr3);  // false
    qDebug() << "Is testStr4 an integer?" << isInteger(testStr4);  // false

    return 0;
}
判断是否为浮点数
cpp 复制代码
bool isFloat(const QString &str) {
    QRegularExpression re("^-?\\d+\\.\\d+$");
    QRegularExpressionMatch match = re.match(str);
    return match.hasMatch();
}

int main() {
    QString testStr1 = "123.45";
    QString testStr2 = "-456.78";
    QString testStr3 = "123";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 a float?" << isFloat(testStr1);  // true
    qDebug() << "Is testStr2 a float?" << isFloat(testStr2);  // true
    qDebug() << "Is testStr3 a float?" << isFloat(testStr3);  // false
    qDebug() << "Is testStr4 a float?" << isFloat(testStr4);  // false

    return 0;
}

2. 使用 QString 的 toInt 和 toDouble 方法

另一种方法是尝试将字符串转换为整数或浮点数,并检查转换是否成功。

判断是否为数字(整数或浮点数)
cpp 复制代码
bool isNumber(const QString &str) {
    bool ok;
    str.toDouble(&ok);
    return ok || str.toInt(&ok) && ok;
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456.78";
    QString testStr3 = "abc";

    qDebug() << "Is testStr1 a number?" << isNumber(testStr1);  // true
    qDebug() << "Is testStr2 a number?" << isNumber(testStr2);  // true
    qDebug() << "Is testStr3 a number?" << isNumber(testStr3);  // false

    return 0;
}
判断是否为整数
cpp 复制代码
bool isInteger(const QString &str) {
    bool ok;
    return str.toInt(&ok) && ok;
}

int main() {
    QString testStr1 = "123";
    QString testStr2 = "-456";
    QString testStr3 = "456.78";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 an integer?" << isInteger(testStr1);  // true
    qDebug() << "Is testStr2 an integer?" << isInteger(testStr2);  // true
    qDebug() << "Is testStr3 an integer?" << isInteger(testStr3);  // false
    qDebug() << "Is testStr4 an integer?" << isInteger(testStr4);  // false

    return 0;
}
判断是否为浮点数
cpp 复制代码
bool isFloat(const QString &str) {
    bool ok;
    return str.toDouble(&ok) && ok;
}

int main() {
    QString testStr1 = "123.45";
    QString testStr2 = "-456.78";
    QString testStr3 = "123";
    QString testStr4 = "abc";

    qDebug() << "Is testStr1 a float?" << isFloat(testStr1);  // true
    qDebug() << "Is testStr2 a float?" << isFloat(testStr2);  // true
    qDebug() << "Is testStr3 a float?" << isFloat(testStr3);  // false
    qDebug() << "Is testStr4 a float?" << isFloat(testStr4);  // false

    return 0;
}

这两种方法各有优缺点,使用正则表达式可以精确匹配字符串模式,而使用 QString 的转换方法则更直观和简洁。选择哪种方法取决于你的具体需求和偏好。

相关推荐
菜鸟看点7 小时前
自定义Cereal XML输出容器节点
c++·qt
漫步企鹅7 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
new_zhou8 小时前
Windows qt打包编译好的程序
开发语言·windows·qt·打包程序
看到我,请让我去学习16 小时前
Qt编程-qml操作(js,c++,canvas)
开发语言·qt
哈市雪花17 小时前
相机:Camera原理讲解(使用OpenGL+QT开发三维CAD)
qt·3d·交互·相机·图形学·opengl·视角
津津有味道19 小时前
Qt C++串口SerialPort通讯发送指令读写NFC M1卡
linux·c++·qt·串口通信·serial·m1·nfc
feiyangqingyun21 小时前
全网唯一/Qt结合ffmpeg实现手机端采集摄像头推流到rtsp或rtmp/可切换前置后置摄像头/指定分辨率帧率
qt·智能手机·ffmpeg
随意02321 小时前
Qt 事件
开发语言·qt
鸥梨菌Honevid1 天前
Qt自定义控件(1)——QPaintEvent
开发语言·qt
Mr_Xuhhh2 天前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法