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

相关推荐
Mr_Xuhhh17 小时前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
feiyangqingyun20 小时前
Qt音视频开发技巧/推流带旋转角度/rtsprtmp推流/保存文件到MP4/拉流解析旋转角度
qt·音视频·qt旋转角度推流
清醒的兰21 小时前
Qt 基于TCP套接字编程
网络·qt·tcp
mahuifa10 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
小灰灰搞电子10 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
Echo``10 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉
智驾11 天前
QML革命:下一代GUI开发的核心优势详解
qt·qml
五_谷_丰_登11 天前
mongoDB服务本地化部署
数据库·c++·qt·mongodb
tangchao340勤奋的老年?11 天前
Qt QMap数据清除测试(验证QMap内存正确释放方法)
开发语言·qt
攻城狮7号11 天前
【AI时代速通QT】第二节:Qt SDK 的目录介绍和第一个Qt Creator项目
c语言·c++·qt·跨平台