Important: Please read the Qt Code of Conduct -

QString - secondIndexOf

  • I'm trying to get the second index of a character (;) in QString, but the only voids I'm aware of are
    Qstring.indexOf() and QString.lastIndexOf().
    Is there any way to find the second index of a semicolon?
    For example, Qstring is: file001;15-02-2019;Accounting;Management; .
    I'm trying to get the index of 2nd semicolon (the one after 2019). How is that possible?

  • Moderators

    @jellyv indexOf() excepts a start index as 2nd parameter and the documentation shows it


    int firstIndex = myString.index(';');
    int 2ndIndex = myString.index(';',firstIndex+1);

    that said, what do you want to do with the index. If you want to extract the strings between ';'

    you could use split or splitref for example.

  • @J-Hilk
    I've tried int 2ndIndex = myString.index(';',firstIndex+1); method but it's useless when you try to find 3rd , 4th and other indexes of a character.
    Split is what I actually needed in my case, thank you!

  • @J-Hilk UPDATE:
    section actually is the best way to get a section of a string which is separated by characters

Log in to reply