gpt4 book ai didi

c++ - 从字母数字 QString 中提取数字

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:02 26 4
gpt4 key购买 nike

我有一个 "s150 d300" 的 QString。如何从 QString 中获取数字并将其转换为整数。简单地使用“toInt”是行不通的。

比方说,从"s150 d300" 的QString 中,只有字母'd' 之后的数字 对我有意义。那么如何从字符串中提取'300'的值呢?

非常感谢您的宝贵时间。

最佳答案

如果你能做到,为什么要这么麻烦:

#include <QDebug>
#include <QString>

const auto serialNumberStr = QStringLiteral("s150 d300");

int main()
{
const QRegExp rx(QLatin1Literal("[^0-9]+"));
const auto&& parts = serialNumberStr.split(rx, QString::SkipEmptyParts);

qDebug() << "2nd nbr:" << parts[1];
}

打印出来:2nd nbr: "300"

关于c++ - 从字母数字 QString 中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45317583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com