Converting one element from a QString to int [SOLVED]
-
wrote on 7 Mar 2012, 19:30 last edited by
Let's say for example we have
@
QString myString = "A1B";
@If I want to convert the second element of QString the "1" into an int, what is the best way to do that. I tried @myString[1].toInt()@ but that didnt work. Thanks
-
wrote on 7 Mar 2012, 19:34 last edited by
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();
@ -
wrote on 7 Mar 2012, 19:42 last edited by
[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
-
wrote on 7 Mar 2012, 19:46 last edited by
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.
-
wrote on 7 Mar 2012, 19:55 last edited by
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 } }@
-
wrote on 7 Mar 2012, 19:59 last edited by
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
-
wrote on 7 Mar 2012, 20:27 last edited by
Be sure and edit your first post to add [Solved] to the thread title! Thanks!
1/7