我有一个 "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/