Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString - Get the characters from Index X to Index Y

QString - Get the characters from Index X to Index Y

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.4k Views
  • 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.
  • J Offline
    J Offline
    jellyv
    wrote on 6 Mar 2020, 19:11 last edited by
    #1

    Hi, I'm trying to figure out how to get the String's characters from index X to index Y.
    For example:

    Qstring filename = "blabla_12345.txt"
    int dotIndex = filename.lastIndexOf(".");
    

    now I want to get the characters from index X till the dotIndex.
    For example, I want to have a Qstring fileNumber , which contains the characters from 6 to dotIndex ("_12345").
    Or in other case, I want to have a QString that contains characters from 2 to dotIndex ("abla_12345")

    P.S (I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.)

    R 1 Reply Last reply 6 Mar 2020, 19:20
    0
    • J jellyv
      6 Mar 2020, 19:11

      Hi, I'm trying to figure out how to get the String's characters from index X to index Y.
      For example:

      Qstring filename = "blabla_12345.txt"
      int dotIndex = filename.lastIndexOf(".");
      

      now I want to get the characters from index X till the dotIndex.
      For example, I want to have a Qstring fileNumber , which contains the characters from 6 to dotIndex ("_12345").
      Or in other case, I want to have a QString that contains characters from 2 to dotIndex ("abla_12345")

      P.S (I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.)

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 6 Mar 2020, 19:20 last edited by
      #2

      @jellyv
      https://doc.qt.io/qt-5/qstring.html

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • J Offline
        J Offline
        jellyv
        wrote on 6 Mar 2020, 20:25 last edited by
        #3

        Yes I've seen the documentation but which of these functions allow me to do exactly what I want?
        As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.

        R 1 Reply Last reply 6 Mar 2020, 21:12
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 6 Mar 2020, 20:40 last edited by
          #4

          @jellyv said in QString - Get the characters from Index X to Index Y:

          As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.

          So can you show us what you tried?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          J 1 Reply Last reply 9 Mar 2020, 15:33
          0
          • J jellyv
            6 Mar 2020, 20:25

            Yes I've seen the documentation but which of these functions allow me to do exactly what I want?
            As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 6 Mar 2020, 21:12 last edited by
            #5

            @jellyv said in QString - Get the characters from Index X to Index Y:

            Yes I've seen the documentation but which of these functions allow me to do exactly what I want?

            QString::mid()?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • C Christian Ehrlicher
              6 Mar 2020, 20:40

              @jellyv said in QString - Get the characters from Index X to Index Y:

              As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.

              So can you show us what you tried?

              J Offline
              J Offline
              jellyv
              wrote on 9 Mar 2020, 15:33 last edited by jellyv 3 Sept 2020, 15:35
              #6

              @Christian-Ehrlicher
              For example, I want to have a Qstring fileNumber , which contains the characters from 7 to dotIndex ("_12345").
              In this case, I had to find the dotIndex, get the string from beginning to dotindex, and then get the string from 6th character till the end.

                  int dotIndex = filename.lastIndexOf(".");
                  QString withoutDot = filename.mid(0, dotIndex);
                  QString fileNumber = withoutDot.mid(7, withoutDot.length());
              

              What I WANT to do is to get the string from X-th (7 in this case) character till the dotIndex in one command.
              In my program, X changes, but dotIndex always remains the same.
              For example, when X is 4, I want to get the string from 4th character till the dotIndex

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 9 Mar 2020, 16:56 last edited by
                #7

                @raven-worx already gave you a hint how to do it - QString::mid() is the way to go.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 9 Mar 2020, 17:52
                0
                • C Christian Ehrlicher
                  9 Mar 2020, 16:56

                  @raven-worx already gave you a hint how to do it - QString::mid() is the way to go.

                  J Offline
                  J Offline
                  jellyv
                  wrote on 9 Mar 2020, 17:52 last edited by
                  #8

                  @Christian-Ehrlicher
                  I don't think I've made myself clear here....

                  Take this example:

                  QString x = "Nine pineapples";
                  QString y = x.mid(5, 4);            // y == "pine"
                  

                  QString::mid() lets you get 4 characters after the 5fth character (pine)
                  I'm looking for a void where you can say: give me characters 5 to 10 (pineap) (5th, 6th, 7th, 8th, 9th and 10th characters)
                  In other words, I'm looking for QString::mid()'s alternative which lets you specify the ending position

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 9 Mar 2020, 18:06 last edited by Christian Ehrlicher 3 Sept 2020, 18:06
                    #9

                    @jellyv said in QString - Get the characters from Index X to Index Y:

                    alternative which lets you specify the ending position

                    mid(start, len) = mid (start, end - len + 1) - simply math, isn't it?
                    mid(5, 6) = mid(5, 10 - 6 + 1)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    3
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 9 Mar 2020, 18:34 last edited by VRonin 3 Sept 2020, 18:36
                      #10

                      If you want to be fancy: QStringView fileNumber(filename.cbegin()+undercorIndex+1,filename.cbegin()+dotIndex);

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1

                      5/10

                      6 Mar 2020, 21:12

                      • Login

                      • Login or register to search.
                      5 out of 10
                      • First post
                        5/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved