Search code examples
c++qtqt4

Slice Qstring from right hand side


I have a qstring of directory structure and need to take the second last element after "/" into a new qstring

"C:/Users/emb/Documents/AutoConnectTest/02/Job_0"

How to save 02 into a new QString


Solution

  • You can use QString split function.

    Splits the string into substrings wherever sep occurs, and returns the list of those strings.

    this code split all section by "/" and merge two second from last.

    QString string = "C:/Users/emb/Documents/AutoConnectTest/02/Job_0"; 
    QStringList lst = string.split('/');
    qDebug() << lst[lst.count()-2] + "/" + lst[lst.count()-1];
    

    Output:

    "02/Job_0"