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 的转换方法则更直观和简洁。选择哪种方法取决于你的具体需求和偏好。

相关推荐
yaso_zhang4 小时前
当生产了~/qt-arm/bin/qmake,可以单独编译其他-源码的某个模块,如下,编译/qtmultimedia
qt
code bean4 小时前
【Qt/C++】深入理解 Lambda 表达式与 `mutable` 关键字的使用
开发语言·c++·qt
爱看书的小沐17 小时前
【小沐学GIS】基于C++绘制二维瓦片地图2D Map(QT、OpenGL、GIS)
c++·qt·gis·opengl·glfw·glut·二维地图
炬火初现1 天前
Qt 的原理及使用(1)——qt的背景及安装
开发语言·qt
weixin_1101 天前
Qt 无边框窗口,支持贴边分屏
c++·qt
gaoenyang7605251 天前
QT Creator配置Kit
开发语言·qt
3D打印-HUSTAIBO1 天前
QT中connect高级链接——指针、lambda、宏
qt
刘梓谦1 天前
Qt获取CPU使用率及内存占用大小
开发语言·c++·qt
追烽少年x2 天前
Qt中在子线程中刷新UI的方法
qt
猫生鱼2 天前
qml中的TextArea使用QSyntaxHighlighter显示高亮语法
c++·qt