关于同一个问题,我看到了一个答案,但是当我尝试解决我的问题时,它变得很困惑。
这是我的字符串,
bit_rate = "bitrate: 2334 kb/s"
我需要从这个字符串中获取 2334
并将其分配给一个整数变量..
这在 qt creator 中怎么可能。我试过 toInt()
,但它总是给出 0
作为答案。
最佳答案
您需要去掉 bitrate:
前缀和 kb/s
后缀。最好的方法是使用QRegExp
或QRegularExpression
提取字符串的数字部分,然后调用toInt()
。
例子如下:
QString str = "bitrate: 3543 kb/s";
int value;
QRegExp re("bitrate:\\s*(\\d+)\\s*.*");
if (re.indexIn(str) != -1) {
value = re.cap(1).toInt();
} else {
qDebug() << "String not matched.";
return;
}
qDebug() << value;
关于c++ - 如何在qt creator中将QString转换成int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630850/