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. Compare single QString against many, evaluate to true if it matches any
Qt 6.11 is out! See what's new in the release blog

Compare single QString against many, evaluate to true if it matches any

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.0k 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.
  • E Offline
    E Offline
    Ewan Green
    wrote on last edited by Ewan Green
    #1

    If I wanted to compare 1 string against any string in a list at once, what could I do besides QStringList::contains()?
    Logically, there's always the ability to test the strings one at a time, but Qt's extensive conveniences for strings makes me think there has to be a "better" (more convenient) way.

    Looking for something like this:
    if(str1 == QString(str2 || str3 || str4))
    Could be inclusive or (|), comma, etc.. I just don't know what to look for.

    Ewan Green

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

      Hi,

      What kind of string are you using for the comparison ?
      How long can that list be ?

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

      E 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What kind of string are you using for the comparison ?
        How long can that list be ?

        E Offline
        E Offline
        Ewan Green
        wrote on last edited by
        #3

        QString. Ideally, the list could be as long as I want, i.e. a variadic static method or something

        Ewan Green

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

          What I meant is the content of the string not the object type :-)

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

          E 1 Reply Last reply
          0
          • SGaistS SGaist

            What I meant is the content of the string not the object type :-)

            E Offline
            E Offline
            Ewan Green
            wrote on last edited by
            #5

            @SGaist I suppose they would all have to be the same length for the comparison to work properly.

            Ewan Green

            1 Reply Last reply
            0
            • E Ewan Green

              If I wanted to compare 1 string against any string in a list at once, what could I do besides QStringList::contains()?
              Logically, there's always the ability to test the strings one at a time, but Qt's extensive conveniences for strings makes me think there has to be a "better" (more convenient) way.

              Looking for something like this:
              if(str1 == QString(str2 || str3 || str4))
              Could be inclusive or (|), comma, etc.. I just don't know what to look for.

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

              @Ewan-Green said in Compare single QString against many, evaluate to true if it matches any:

              if(str1 == QString(str2 || str3 || str4))

              With this intention/syntax, makes me think of regular expression (use QRegularExpression): (str2|str3|str4). I guess it's one way of reducing typing a bit. This is only for literals, as per your code, unless you meant variables, but QString(str2 || str3 || str4) does not imply that to me.

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

                If it's an exact match you are looking for then QStringList::indexOf looks like what you want.

                If you want it search whether there's an occurrence of your string within an entry of the list then using this overload with a simple regexp should do the trick.

                And no there's no need for the strings to be all the same length.

                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
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by Kent-Dorfman
                  #8

                  if using a static or pre-loaded list of strings then create a map and compare your input string against the map keys using map::find(). Your search times will generally be much shorter. the general mechanism here is to use a binary search of a sorted list to determine if your input string is found.

                  The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                  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