Converting one element from a QString to int [SOLVED]
-
toInt() works when the string it's a number, but you have A,B, you can read the documentation about QString in http://qt-project.org/doc/qt-4.8/QString.html
if you know that the number it's always in the second position you could do something like this:
@
QString myString = "A1B";
int number = myString.mid(1,1).toInt();
@ -
[quote author="Iktwo" date="1331148873"]toInt() works when the string it's a number, but you have A,B, you can read the documentation about QString in http://qt-project.org/doc/qt-4.8/QString.html
if you know that the number it's always in the second position you could do something like this:
@
QString myString = "A1B";
int number = myString.mid(1,1).toInt();
@
[/quote]Thanks for you reply, what i meant to say (but showed up as a reference) is i tried
@
int myInt = myString[1].toInt();
@but that didnt work.
I dont know the exact position of the numbers in the QString. In fact what I would like to do is loop through all elements and convert the ones that are numbers
-
How do you intend to store multiple numbers (if the string contains more than one)? Originally you were trying to store the results in a single int variable.
Also, are you trying to convert single digits? Or whole numbers? (That is, does "A123B" need to parse to 123, or to the series of numbers 1, 2, and 3?
The ultimate answer is going to probably involve parsing the string (perhaps with a QRegExp) and extracting the individual numbers you want to convert, then iteratively doing the string->integer conversion on the extracted strings.
-
I don't understand what you need, if you have strings like:
"A1B0H1H3J4"and you want:
1 0 1 3 4you could do something like this:
@QString myString = "A1B0H1H3J4";
int number = myString.mid(1,1).toInt();for (int var = 0; var < myString.length(); ++var) { bool ok; if (myString.at(var).isDigit()){ int digit = myString.at(var).digitValue(); //DO SOMETHING HERE WITH THE DIGIT } }@
-
Tha
[quote author="Iktwo" date="1331150150"]I don't understand what you need, if you have strings like:
"A1B0H1H3J4"and you want:
1 0 1 3 4you could do something like this:
@
QString myString = "A1B0H1H3J4";
int number = myString.mid(1,1).toInt();for (int var = 0; var < myString.length(); ++var) { bool ok; if (myString.at(var).isDigit()){ int digit = myString.at(var).digitValue(); //DO SOMETHING HERE WITH THE DIGIT } }@
[/quote]
Thanks that is exactly what i needed