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

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 481 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.
  • E Offline
    E Offline
    Ewan Green
    wrote on 25 Sept 2021, 17:20 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

    J 1 Reply Last reply 25 Sept 2021, 18:24
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Sept 2021, 17:53 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 25 Sept 2021, 17:55
      0
      • S SGaist
        25 Sept 2021, 17:53

        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 25 Sept 2021, 17:55 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Sept 2021, 17:56 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 25 Sept 2021, 18:01
          0
          • S SGaist
            25 Sept 2021, 17:56

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

            E Offline
            E Offline
            Ewan Green
            wrote on 25 Sept 2021, 18:01 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
              25 Sept 2021, 17:20

              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.

              J Offline
              J Offline
              JonB
              wrote on 25 Sept 2021, 18:24 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 25 Sept 2021, 18:35 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
                • K Offline
                  K Offline
                  Kent-Dorfman
                  wrote on 27 Sept 2021, 03:27 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.

                  1 Reply Last reply
                  0

                  1/8

                  25 Sept 2021, 17:20

                  • Login

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