Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get index of a SubString contained in a QStringList

Get index of a SubString contained in a QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by
    #1

    Hello,

    I come to you because I have the following problem:
    I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
    In the QStringList there can only be one error, randomly placed in the list.
    I'm looking for a way to make the error of this QStringList to always be at index 0.
    One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
    But I just want to find the SubString "Error"

    I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day

    jsulmJ JonBJ 2 Replies Last reply
    0
    • P Pantoufle

      Hello,

      I come to you because I have the following problem:
      I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
      In the QStringList there can only be one error, randomly placed in the list.
      I'm looking for a way to make the error of this QStringList to always be at index 0.
      One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
      But I just want to find the SubString "Error"

      I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Pantoufle said in Get index of a SubString contained in a QStringList:

      function only seems to work if the string is exactly equal to what i search

      Well, your string contains Error and you're searching for ERROR, right? Why? But if you want to do so, then simply set the cs parameter in https://doc.qt.io/qt-6/qstring.html#indexOf to Qt::CaseInsensitive

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Pantoufle

        Hello,

        I come to you because I have the following problem:
        I have a QStringList of Error and Warning. Warnings are written like this: "Warning: WARN_CODE" and error like this "Error: ERR_CODE"
        In the QStringList there can only be one error, randomly placed in the list.
        I'm looking for a way to make the error of this QStringList to always be at index 0.
        One of the solutions I would have liked to implement would have been to find the index of the QString containing "ERROR" and simply put it in the first position, but the "indexOf" function only seems to work if the string is exactly equal to what i search.
        But I just want to find the SubString "Error"

        I tried various method with QStringList::Filter or QStringList::indexOf without success. What would be the "clean" way to do this? Thanks in advance, have a good day

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Pantoufle
        You can either

        • Iterate the string list looking for QString::indexOf() in each one.

        • Use qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const to find a suitable QRegularExpression which you construct to find your desired substring. Probably something like .*Error.*.

        P 1 Reply Last reply
        1
        • P Offline
          P Offline
          Pantoufle
          wrote on last edited by
          #4

          Excuse me I wrote badly in the post but no I have "ERROR: ERR_XXX" in my QStringList and I'm looking for "ERROR" with indexof.
          Example:

              QStringList errAndWarn;
              errAndWarn.append("WARNING: WARN_FFF");
              errAndWarn.append("ERROR: ERR_FFF");
              errAndWarn.append("WARNING: WARN_FFF");
          
              std::cout << errAndWarn.indexOf("ERROR") << std::endl;
          

          This little sample return me "-1" as indexOf search for the exact match.

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @Pantoufle
            You can either

            • Iterate the string list looking for QString::indexOf() in each one.

            • Use qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const to find a suitable QRegularExpression which you construct to find your desired substring. Probably something like .*Error.*.

            P Offline
            P Offline
            Pantoufle
            wrote on last edited by
            #5

            @JonB

            Will try this, thanks

            JonBJ 1 Reply Last reply
            0
            • P Pantoufle

              @JonB

              Will try this, thanks

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Pantoufle
              That because the argument is a regular expression and it says

              Returns the index position of the first exact match of re in the list

              Try my suggested reg ex, see how the .* gobble up other characters so that it (should) match exactly. Your case might also work with ERROR:.*. You may also want to make the reg exp case insensitive, depending on your inputs.

              1 Reply Last reply
              0
              • P Pantoufle

                Excuse me I wrote badly in the post but no I have "ERROR: ERR_XXX" in my QStringList and I'm looking for "ERROR" with indexof.
                Example:

                    QStringList errAndWarn;
                    errAndWarn.append("WARNING: WARN_FFF");
                    errAndWarn.append("ERROR: ERR_FFF");
                    errAndWarn.append("WARNING: WARN_FFF");
                
                    std::cout << errAndWarn.indexOf("ERROR") << std::endl;
                

                This little sample return me "-1" as indexOf search for the exact match.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Pantoufle OK, you're searching in the list directly. Then do what @JonB suggested:

                qDebug() << errAndWarn.indexOf(QRegularExpression(".*ERROR.*"));
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Pantoufle
                  wrote on last edited by
                  #8

                  I just tested, it works perfectly, thank a lot and have a nice day

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved