Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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#\\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 !



  • 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#\\\\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.


Log in to reply