[SOLVED] QRegExp : remove last character and get path



  • Hi,

    Pretty much everything is in the title. I'm losing my mind with QRegExp : I tried so many things that I don't know what should I do to solve this.

    I want to do two things:

    1 - Remove a specific character "}" but only when it is at the end of the QStringList. I tried stuff like : @list.replaceInStrings("}$", "");@

    2 - Get the path from a QStringList like "C:\Users\Doe\Desktop\Aaaah.txt" without the filename.

    Thanks for your help !


  • Moderators

    Hi,

    bq. 1 – Remove a specific character “}” but only when it is at the end of the QStringList. I tried stuff like :
    list.replaceInStrings("}$", "");

    You can get last item of the QStringList and check if it is “}” and then remove it from QStringList.

    bq. 2 – Get the path from a QStringList like “C:\Users\Doe\Desktop\Aaaah.txt” without the filename.

    Try

    @QString path = "C:\Users\Doe\Desktop\Aaaah.txt";

    qDebug() << path.section("\",0,-2);
    @



  • Thanks for your answer !

    bq. You can get last item of the QStringList and check if it is “}” and then remove it from QStringList

    Yes I could do that, but I don't want to check every element one by one. There must be a nice solution using QRegExp !?

    Your solution for the path is perfect !


  • Moderators

    Ok, Just misunderstood your first question.

    Try this,
    @list = list.replaceInStrings(QRegExp("[}]$"),QString(""));@
    You need to include it in square brackets.



  • Nop, it doesn't work, nice try though !



  • My bad, your solution works !
    Thanks !


  • Moderators

    Your welcome :)
    You can mark the thread as solved. Just edit the title and prepend it with [solved]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.