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. Searching for the last substring after "\"

Searching for the last substring after "\"

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 6.2k 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.
  • M Offline
    M Offline
    mulfycrowh
    wrote on last edited by
    #1

    Hi,

    Fo example, I have the directory "C:\examples\toys\vehicles\cars".
    What would you suggest to use to get the substring "cars" ?
    Any function in QDir or QString ?

    Many thanks

    Joel BodenmannJ the_T 2 Replies Last reply
    0
    • M mulfycrowh

      Hi,

      Fo example, I have the directory "C:\examples\toys\vehicles\cars".
      What would you suggest to use to get the substring "cars" ?
      Any function in QDir or QString ?

      Many thanks

      Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by
      #2

      Sounds like QDir::dirName is pretty much what you want.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      1
      • M mulfycrowh

        Hi,

        Fo example, I have the directory "C:\examples\toys\vehicles\cars".
        What would you suggest to use to get the substring "cars" ?
        Any function in QDir or QString ?

        Many thanks

        the_T Offline
        the_T Offline
        the_
        wrote on last edited by
        #3

        @mulfycrowh

        There are several ways to do so.

        One way could be to split the string with QString::split() which will return a QStringList. QStringList::last() will give you then the last element of this list, (you should check the size of the list if its >0)

        -- No support in PM --

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mulfycrowh
          wrote on last edited by
          #4

          Finally I use lastIndexOf on the QString to get the position of the last ""

          Joel BodenmannJ 1 Reply Last reply
          0
          • M mulfycrowh

            Finally I use lastIndexOf on the QString to get the position of the last ""

            Joel BodenmannJ Offline
            Joel BodenmannJ Offline
            Joel Bodenmann
            wrote on last edited by
            #5

            @mulfycrowh said:

            Finally I use lastIndexOf on the QString to get the position of the last ""

            In case of you are dealing with file system paths I strongly recommend using QDir::dirName. It has been purposely-designed for that task.
            Your method will already stop working if the system you're running on uses a different file path separator. You could use QDir::Separator() and still use your method of extracting but you might hit other problems down the track in the future.

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            1 Reply Last reply
            1
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              QString has a specialized function exactly for this kind of task (works for any string and separator, not only paths): section():

              QString path("C:\\examples\\toys\\vehicles\\cars");
              QString last_part = path.section('\\', -1);
              

              @the_ Please don't ever use split() for things like this. It creates a dynamically allocated linked list of dynamically allocated copies of parts of the string and then copies the last one. That's probably the slowest and most memory consuming way to do this.

              1 Reply Last reply
              1
              • M Offline
                M Offline
                mulfycrowh
                wrote on last edited by
                #7

                Thanks. It runs perfectly

                Joel BodenmannJ 1 Reply Last reply
                0
                • M mulfycrowh

                  Thanks. It runs perfectly

                  Joel BodenmannJ Offline
                  Joel BodenmannJ Offline
                  Joel Bodenmann
                  wrote on last edited by Joel Bodenmann
                  #8

                  If you're not going to use QDir::dirName() please at least use QDir::Separator() in your QString::section() function. Do yourself a favor :)
                  Otherwise it won't work on any system that uses a separator other than \ (which is almost every system except Windows).

                  Industrial process automation software: https://simulton.com
                  Embedded Graphics & GUI library: https://ugfx.io

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mulfycrowh
                    wrote on last edited by
                    #9

                    I mean with dirName

                    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 Extensions
                    • Unsolved