Extracting file path from String

  • 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#\\\project\name\file.pdf#".
    I want the output as "\\project\name\file.pdf"

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

    "\ project name file.pdf".

    Please Help !

  • This post is deleted!

  • @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.

  • @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 !

  • 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#\\\\\\project\\name\\file.pdf#";
    qDebug().noquote() << str;
    qDebug().noquote() << str.section('#',1,1);


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


Log in to reply