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. Matching QString variables
Qt 6.11 is out! See what's new in the release blog

Matching QString variables

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.2k Views 2 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.
  • B Offline
    B Offline
    Buller
    wrote on last edited by
    #1

    What would be the best way to find the number of matching chars in two QString variables, starting from the left?
    So if I compare "van der Dam is at home" and "van der Dom is at home" is should get 9. Although it would not difficult to write such a function I would rather use a Qt-provided function if available. However, QString::Compare does not seem to fit the bill. Is QRegExp the way to go?

    JKSHJ Pablo J. RoginaP JonBJ 3 Replies Last reply
    0
    • B Buller

      What would be the best way to find the number of matching chars in two QString variables, starting from the left?
      So if I compare "van der Dam is at home" and "van der Dom is at home" is should get 9. Although it would not difficult to write such a function I would rather use a Qt-provided function if available. However, QString::Compare does not seem to fit the bill. Is QRegExp the way to go?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Buller said in Matching QString variables:

      Although it would not difficult to write such a function I would rather use a Qt-provided function if available.

      There is no Qt-provided function for finding common substrings, I'm afraid.

      The best was forward is to write your own function.

      QString::Compare does not seem to fit the bill.

      No. QString::compare() tells you if one string is lexicographically greater, equal to, or less than another string. It doesn't find a common substring between 2 strings.

      Is QRegExp the way to go?

      No. A regular expression (see https://regexr.com/ ) lets you specify a pattern to match within a longer string. It doesn't find a common substring between 2 strings.

      Anyway, QRegExp is deprecated. New code should use QRegularExpression.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      3
      • B Buller

        What would be the best way to find the number of matching chars in two QString variables, starting from the left?
        So if I compare "van der Dam is at home" and "van der Dom is at home" is should get 9. Although it would not difficult to write such a function I would rather use a Qt-provided function if available. However, QString::Compare does not seem to fit the bill. Is QRegExp the way to go?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Buller for searching the common longest prefix you may want to consider using a prefix trie data structure.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • B Buller

          What would be the best way to find the number of matching chars in two QString variables, starting from the left?
          So if I compare "van der Dam is at home" and "van der Dom is at home" is should get 9. Although it would not difficult to write such a function I would rather use a Qt-provided function if available. However, QString::Compare does not seem to fit the bill. Is QRegExp the way to go?

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

          @Buller
          Just march left to right through both. It's O(n) (I would have thought O(2n)) which is about as good as you're going to get. If you feel like optimizing, treat the buffers as int32 * (or even int64 * if you're all 64-bit), that can divide the time by 2 or 4 :)

          1 Reply Last reply
          4
          • B Offline
            B Offline
            Buller
            wrote on last edited by
            #5

            Thanks for the suggestions. This is not really a problem, I just expected Qt to have a "standard" way of dealing with this task. Perhaps some form of convenience function could be added to QSting in a future version?

            Pablo J. RoginaP 1 Reply Last reply
            0
            • B Buller

              Thanks for the suggestions. This is not really a problem, I just expected Qt to have a "standard" way of dealing with this task. Perhaps some form of convenience function could be added to QSting in a future version?

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Buller said in Matching QString variables:

              Perhaps some form of convenience function could be added to QSting in a future version?

              You're more than welcome to contribute a patch! :-)

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1

              • Login

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