[英]How can I convert a QString of numbers to an array of int?
I have a QString
containing a series of numbers for example 我有一个包含一系列数字的QString
QString path = "11100332001 234 554 9394";
I want to iterate over the variable length string 我想迭代可变长度字符串
for (int i=0; i<path.length(); i++){
}
and be able to acces each number as an int
individually. 并能够单独访问每个数字作为int
。
I haven't been able to do so however. 但是,我无法这样做。 Question : How can I convert a QString
of numbers to an array of int
? 问题 :如何将数字的QString
转换为int
数组?
I know I can convert the QString
to an int
using path.toInt()
but that doesn't help me. 我知道我可以使用path.toInt()
将QString
转换为int
但这对我没有帮助。
When trying to convert it to a char first I get an error: cannot convert 'const QChar to char'
. 当我尝试将它转换为char时,我得到一个错误: cannot convert 'const QChar to char'
。
for (int i=0; i<path.length(); i++){
char c = path.at(i);
int j = atoi(&c);
//player->movePlayer(direction);
}
you can use split
to obtain an array of substrings separated by the separator you want in this case it's a space , here is an example : 你可以使用split
来获取由你想要的分隔符分隔的子串数组,在这种情况下它是一个空格,这是一个例子:
QString str("123 457 89");
QStringList list = str.split(" ",QString::SkipEmptyParts);
foreach(QString num, list)
cout << num.toInt() << endl;
您可以使用[]运算符获取每个字符(QChar)并对它们使用digitValue()方法来获取整数。
Use QTextStream : 使用QTextStream :
QString str = "123 234 23123 432";
QTextStream stream(&str);
QList<int> array;
while (!stream.atEnd()) {
int number;
stream >> number;
array.append(number);
}
I have used the answer by Antoyo myself but I ran into an issue today. 我自己使用了Antoyo的答案,但我今天遇到了一个问题。 digitValue() is not the same as comparing a char in C using atoi like the op Thomas was trying to do. digitValue()与使用atoi比较C中的char不同,就像托马斯试图做的那样。 So while Thomas was trying to do this: 因此,虽然托马斯试图这样做:
for (int i=0; i<path.length(); i++){
char c = path.at(i);
int j = atoi(&c);
//player->movePlayer(direction);
}
with digitValue() it would have become this: 使用digitValue()它会变成这样:
for (int i=0; i<path.length(); i++){
int j = path.at(i).digitValue();
//player->movePlayer(direction);
}
However this does not have the expected results if there are any letters in the string. 但是,如果字符串中有任何字母,则不会产生预期的结果。 For example in C, if the string has a mix of Letters and numbers then any char you use atoi on will return a 0 in place of a letter. 例如,在C中,如果字符串混合了字母和数字,那么使用atoi on的任何字符都将返回0代替字母。 digitValue() however does not return 0 for a letter. 但是,对于一个字母,digitValue()不会返回0。 It may be a good idea to do a check for isDigit() first if you have any concerns about letters ending up in your string and the final result would look like this: 如果您对字符串中的字母有任何疑虑并且最终结果如下所示,那么首先检查isDigit()可能是个好主意:
int j = 0;
for (int i=0; i<path.length(); i++){
if (path.at(i).isDigit())
j = path.at(i).digitValue();
else
j = 0;
//player->movePlayer(direction);
}
This may not have been a concern for the OP but since this is one of the top Google results for getting numbers from a string in QT it might help someone else, especially if you're trying to get a QT application to behave the same as some embedded C code on a device. 这可能不是OP的关注点,但由于这是谷歌在QT中从字符串中获取数字的最佳结果之一,它可能对其他人有所帮助,特别是如果你试图使QT应用程序的行为与设备上的一些嵌入式C代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.