Solved: Beginner: get character from string and then convert it to value



  • Hello guys,

    I have a string as follows R3_1, I want to get the 3 and the 1 as numbers
    In VB i used to do it using mid("R3_1",2,1) and then val ("3")

    how do you do it in QT? Is there an alternative for the mid and val functions in VB?

    thank you


  • Moderators

    A "QString":http://qt-project.org/doc/qstring.html can do what you want :)

    By the way, "QT" is an Apple product. Write "Qt" instead.



  • Please give me an example ... I know that QString can do it but I am still begineer and need an example.



  • Well... you could of course see if you can find similar methods as you were using in VB in QString as well. Like, mid() and toInt().

    There are plenty of other methods to do this as well, some much more flexible or robust than your method. It depends on the exact format you need to parse though: how sure are you that this is your exact format? Can there be numbers longer than a single digit in there? Can the R become a different character? Or become multiple characters?



  • to whom it may interest later

    @
    QString keystring="R3_1";
    QString rowc=keystring.mid(1,1); //3
    int row = keyc.toInt(); //3
    int y=row+1; //4
    @


  • Moderators

    [quote author="SherifOmran" date="1348126079"]Please give me an example ... I know that QString can do it but I am still begineer and need an example.[/quote]

    Did you read the link? It already has an example on how to use "QString::mid()":http://qt-project.org/doc/qt-4.8/qstring.html#mid -- it's very similar to VB. Also, reading documentation is good, because it helps you discover new things you didn't think about before.

    Like Andre said, the QString version of val() is "QString::toInt()":http://qt-project.org/doc/qt-4.8/qstring.html#toInt (but there are different functions for different formats, like "QString::toFloat()":http://qt-project.org/doc/qt-4.8/qstring.html#toFloat ) An example is:
    @
    QString numberString = "3";
    int number = numberString.toInt();
    @

    [Edit]: Ah, you beat me to it


  • Moderators

    [quote author="SherifOmran" date="1348127356"]
    @
    QString keystring="R3_1";
    QString rowc=keystring.mid(1,1); //3
    int row = keyc.toInt(); //3
    int y=row+1; //4
    @
    [/quote]
    [quote author="Andre" date="1348126648"]There are plenty of other methods to do this as well, some much more flexible or robust than your method. It depends on the exact format you need to parse though: how sure are you that this is your exact format? Can there be numbers longer than a single digit in there? Can the R become a different character? Or become multiple characters?[/quote]
    What Andre meant was: What happens if you have 11 rows, or 20 columns?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.