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. Exact match in QStringList
Qt 6.11 is out! See what's new in the release blog

Exact match in QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 14.2k Views 3 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by A Former User
    #1

    i have a qstringlist, say

    QStringList list;
    list << "abcd" << "efgh";
    

    and i wanna match an exact string, i.e.

    if(!list.contains(text)) {
        // do something
    }
    

    the thing is, i wanna catch an exact match, for example "acbd", but when i pass "cd" as an argument to contains(), it returns true, since "cd" is contained in "abcd".

    and contains won't match with regexp.

    what can i do?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Then use filter or indexOf with a QRegularExpression.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        but they search for a substring, i need exact match of a string

        mrjjM 1 Reply Last reply
        0
        • U user4592357

          but they search for a substring, i need exact match of a string

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @user4592357
          Hi
          A low tech solution could just be

           QStringList list;
            list << "anders" << "peter" << "hanspeter" << "polka" << "hans";
          
            foreach (const QString& var, list) {
              if ( var == "hans" ) qDebug() << "HIT!";
            }
          
          

          But that dont allow for Case sensitive matching.

          U 1 Reply Last reply
          0
          • mrjjM mrjj

            @user4592357
            Hi
            A low tech solution could just be

             QStringList list;
              list << "anders" << "peter" << "hanspeter" << "polka" << "hans";
            
              foreach (const QString& var, list) {
                if ( var == "hans" ) qDebug() << "HIT!";
              }
            
            

            But that dont allow for Case sensitive matching.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @mrjj

            that's what i currently have. i don't care aboit case sensitivity in my problem. but i wanted a more "sophisticated" method, or to be precise, a shorter and already implemented one

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              There's not even a need for QRegularExpression.

              qDebug() << list.indexOf("cd") << list.indexOf("abcd");
              

              You'll get -1 and 0.

              So you replace your current if condition with list.indexOf(text) == -1 and you're good to go.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              JonBJ 1 Reply Last reply
              5
              • SGaistS SGaist

                There's not even a need for QRegularExpression.

                qDebug() << list.indexOf("cd") << list.indexOf("abcd");
                

                You'll get -1 and 0.

                So you replace your current if condition with list.indexOf(text) == -1 and you're good to go.

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

                @SGaist
                I don't get what you're proposing. Are you saying QStringList::indexOf only finds complete-element matches, not substrings (the docs don't make this clear to me), but the OP used QStringList::contains() and that does not behave same? Plus, docs say that all overloads of QStringList::indexOf take a regular expression type argument, how does list.indexOf("cd") return -1?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by SGaist
                  #8

                  indexOf documentation states: "the first exact match" well except the overload for QRegularExpression but that might be a documentation issue.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It's indeed a documentation issue. It works as the other overload.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JonBJ 1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      user4592357
                      wrote on last edited by
                      #10

                      that is why i was confused by looking at the docs. sorry, anyways.
                      indexOf() does what i was looking for

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        It's indeed a documentation issue. It works as the other overload.

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

                        @SGaist
                        So indexOf, which only accepts a regular expression, does a ^pattern$ ("exact" match?) against each element, and contains does a sub string (across all elements concatenated or each element individually?) ?

                        U 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @SGaist
                          So indexOf, which only accepts a regular expression, does a ^pattern$ ("exact" match?) against each element, and contains does a sub string (across all elements concatenated or each element individually?) ?

                          U Offline
                          U Offline
                          user4592357
                          wrote on last edited by
                          #12

                          @JNBarchan

                          that's what my experience was

                          1 Reply Last reply
                          1
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by SGaist
                            #13

                            Just one thing: you have something strange going on. Contains does an exact string comparison (depending on the case sensitivity chosen) so you should verify that you really did pass only two characters.

                            QStringList list;
                            list << "abcd" << "efgh" << "bcfg";
                            qDebug() << list.indexOf(QRegularExpression("bc")) 
                                     << list.indexOf(QRegularExpression("abcd"));
                            qDebug() << list.contains("bc")
                                     << list.contains("abcd");
                            

                            Returns:

                            -1 0
                            false true
                            

                            so in fact working as expected.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            3

                            • Login

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