C++ 从字母数字QString中提取数字
我有一个Q字符串“s150 d300”。如何从QString中获取数字并将其转换为整数。简单地使用“toInt”是行不通的 比如说,从“s150 d300”的QString中,只有字母“d”后面的数字对我有意义。那么如何从字符串中提取'300'的值呢C++ 从字母数字QString中提取数字,c++,qt,integer,qt5,alphanumeric,C++,Qt,Integer,Qt5,Alphanumeric,我有一个Q字符串“s150 d300”。如何从QString中获取数字并将其转换为整数。简单地使用“toInt”是行不通的 比如说,从“s150 d300”的QString中,只有字母“d”后面的数字对我有意义。那么如何从字符串中提取'300'的值呢 非常感谢您的时间。一种可能的解决方案是使用正则表达式,如下所示: #include <QCoreApplication> #include <QDebug> int main(int argc, char *argv[]
非常感谢您的时间。一种可能的解决方案是使用正则表达式,如下所示:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString str = "s150 dd300s150 d301d302s15";
QRegExp rx("d(\\d+)");
QList<int> list;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
list << rx.cap(1).toInt();
pos += rx.matchedLength();
}
qDebug()<<list;
return a.exec();
}
感谢@IlBeldus的评论,根据QRegExp将提供的信息,因此我建议使用以下解决方案:
另一个解决方案:
QString str = "s150 dd300s150 d301d302s15";
QRegularExpression rx("d(\\d+)");
QList<int> list;
QRegularExpressionMatchIterator i = rx.globalMatch(str);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString word = match.captured(1);
list << word.toInt();
}
qDebug()<<list;
如果您的字符串被拆分为空格分隔的标记,如您给出的示例所示,您只需将其拆分,然后找到满足您需要的标记,然后获取其中的数字部分,即可从中获取值。我在将qstring转换成我更喜欢的东西后使用了atoi,但我认为有一种更有效的方法 尽管这不如正则表达式灵活,但对于您提供的示例,它应该提供更好的性能
#include <QCoreApplication>
int main() {
QString str = "s150 d300";
// foreach " " space separated token in the string
for (QString token : str.split(" "))
// starts with d and has number
if (token[0] == 'd' && token.length() > 1)
// print the number part of it
qDebug() <<atoi(token.toStdString().c_str() + 1);
}
#包括
int main(){
QString str=“s150 d300”;
//字符串中的foreach“”空格分隔标记
对于(QString标记:str.split(“”))
//以d开头,有数字
if(令牌[0]='d'&&token.length()>1)
//打印它的数字部分
qDebug()对于这个问题已经有了合适的解决方案,但是我想强调一下,QString::toInt
将不起作用,因为被转换的字符串应该是数字的文本表示,在给定的示例中,它是非标准记法中的字母数字表达式,因此它是必要的请务必按照建议手动处理,以使Qt执行转换“易于理解”。如果您能做到以下几点,为什么会出现所有问题:
#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];
}
#包括
#包括
常数auto serialNumberStr=QStringLiteral(“s150 d300”);
int main()
{
常数QRegExp rx(QLatin1Literal(“[^0-9]+”);
const auto&&parts=serialNumberStr.split(rx,QString::skipmptyparts);
qDebug()QQReXP在QT5中被弃用,QQualReXPress应该被使用。QT论坛重复的问题:两个版本仍然支持,看这个:而且它不是重复的,我在2个小时前已经回复了,而论坛已经在15分钟前被解决了。我已经在QT5.5.1I中测试了我的解决方案。欢迎您的评论。谢谢。:P
#include <QCoreApplication>
int main() {
QString str = "s150 d300";
// foreach " " space separated token in the string
for (QString token : str.split(" "))
// starts with d and has number
if (token[0] == 'd' && token.length() > 1)
// print the number part of it
qDebug() <<atoi(token.toStdString().c_str() + 1);
}
#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];
}