Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Solved: Beginner: get character from string and then convert it to value
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

            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
            @

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [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?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved