Unsolved Matching QString variables
-
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? -
@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 useQRegularExpression
. -
-
@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 asint32 *
(or evenint64 *
if you're all 64-bit), that can divide the time by 2 or 4 :) -
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?
-
@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! :-)