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. Extracting file path from String
Forum Updated to NodeBB v4.3 + New Features

Extracting file path from String

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views 2 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.
  • G Offline
    G Offline
    Gagan
    wrote on last edited by Gagan
    #1

    Hi everyone,

    I am using Qt 5.8 and having a query regarding extracting a substring from main string.
    here substring is the file path of file on server located in local LAN.

    Actual String is

    QString str = "My comment given for the file#\\192.168.1.1\project\name\file.pdf#".
    I want the output as "\192.168.1.1\project\name\file.pdf"

    I tried with section function and qDebug() to console but its removing backslashes. its showing me like this

    "\192.168.1.1 project name file.pdf".

    Please Help !

    G 1 Reply Last reply
    0
    • G Gagan

      Hi everyone,

      I am using Qt 5.8 and having a query regarding extracting a substring from main string.
      here substring is the file path of file on server located in local LAN.

      Actual String is

      QString str = "My comment given for the file#\\192.168.1.1\project\name\file.pdf#".
      I want the output as "\192.168.1.1\project\name\file.pdf"

      I tried with section function and qDebug() to console but its removing backslashes. its showing me like this

      "\192.168.1.1 project name file.pdf".

      Please Help !

      G Offline
      G Offline
      Gagan
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Gagan Is the value you need always sorrounded by #, like the example you provided? If so you may want to use regular expresions using QRegularExpression.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        G 1 Reply Last reply
        1
        • Pablo J. RoginaP Pablo J. Rogina

          @Gagan Is the value you need always sorrounded by #, like the example you provided? If so you may want to use regular expresions using QRegularExpression.

          G Offline
          G Offline
          Gagan
          wrote on last edited by
          #4

          @Pablo-J.-Rogina
          Thanks so much for your suggestion but m not much familiar with QRegularExpression.
          Still anyways, I have solved the same thing in SQl query itself while loading the resultset.

          Thanks again !

          1 Reply Last reply
          0
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            Hi @Gagan,

            QString::section() should work fine. I suspect you're being tripped up by the need for escaping in string literals?

            Here's a working example:

            QString str = "My comment given for the file#\\\\192.168.1.1\\project\\name\\file.pdf#";
            qDebug().noquote() << str;
            qDebug().noquote() << str.section('#',1,1);
            

            Output:

            My comment given for the file#\\192.168.1.1\project\name\file.pdf#
            \\192.168.1.1\project\name\file.pdf
            

            Cheers.

            1 Reply Last reply
            3

            • Login

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