PDA

View Full Version : QString comparision with some different char



^NyAw^
18th May 2013, 18:41
Hi,

I want to compare to QStrings and I want to know how many chars are different.
I have a QString that I need to search on a QStringList, eg: search "ADLSHERPOA" on a QStringList with elements like "BAHDOMAYTC;GAPCLAUIBG;ADLSHERPOB" that can contain some errors

Have I to use QRexExp or it's better to compare all the chars one by one? Think on that it must be a fast solution(speed is important).

Thanks,

amleto
18th May 2013, 18:53
What is the outcome that you want? Please show some example inputs and outputs.

It is not clear what you mean when you say some strings will contain errors.

^NyAw^
18th May 2013, 19:27
Hi,

QStrinList containing "BAHDOMAYTC;GAPCLAUIBG;ADLSHERPOB"
QString containing "ADLSHERPOA".

I want to search the QString "ADLSHERPOA" on every QStringList trying to find the same element. If the element is different, I want to know how many chars are different. In this case, comparing "ADLSHERPOA" with "BAHDOMAYTC" will return 10 errors. Comparing "ADLSHERPOA" with "GAPCLAUIBG" will also return 10 errors. Comparing "ADLSHERPOA" with "ADLSHERPOB" will return 1 error.

Thanks,

wysota
18th May 2013, 21:41
http://en.wikipedia.org/wiki/Hamming_distance
http://en.wikipedia.org/wiki/Levenshtein_distance

ChrisW67
18th May 2013, 22:07
The options depend a little on the precise nature of your strings and what you mean by the number of differences. Are the strings are always 10 characters long, not actual words, and a difference is counted if corresponding characters are different? Since you want to know the error count in all cases I don't see a generally fast method of doing this. You need to compare every character of every string with corresponding character in the candidate string.

In general terms you are looking for something like Levenshtein distance (http://en.wikipedia.org/wiki/Levenshtein_distance) or a similar measure.

If you are doing this to provide some sort of speling correcshun then you might find this article interesting. http://www.norvig.com/spell-correct.html

wysota
18th May 2013, 22:26
Since you want to know the error count in all cases I don't see a generally fast method of doing this.

It depends whether the comparison is done once or multiple times against the same set of strings. If the latter then a smart structure based on a radix trie (or some other kind of trie) might be able to speed things up.