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. Howto substring using QString
Forum Updated to NodeBB v4.3 + New Features

Howto substring using QString

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 7.0k 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.
  • P Offline
    P Offline
    pingal
    wrote on last edited by pingal
    #1

    I'm used to std::string so doesn't really know much about QString class.

    How can i substring on QString, I will give you my scenerio.

    lets say i've a path i.e. path\to\file.jpg

    In order to extract the filename from the above path, i usually substring using std::string::substr(..) like below:

    std::string path {"path\\to\\file.jpg"};
    std::string filename = path.substr(path.find_last_of("\\")+1);   // file.jpg
    

    How would i do this in QString ?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @pingal said in Howto substring using QString:

      How would i do this in QString ?

      By taking a look in the documentation: QString::mid(), QString::indexOf()

      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
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        If it actually a path that you are manipulating then you should also look at QFileInfo and QDir::fromNativeSeparators.
        It is cross-platform and you can avoid the escaped backslash nastiness.

        1 Reply Last reply
        2
        • P Offline
          P Offline
          pingal
          wrote on last edited by
          #4

          Thanks. That helped

          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