问题
I would like to compare two NSString
s. If the user types "Stonehhengge", should get a "border on answer"="almost equals", because the right answer is: "Stonhenge".
I would like a percentage of how much one string equals another. If "Stonhenge" is 9 letters, and the typed text contain 8 letters from "Stonhenge", but it's not equal to "Stonhenge", for example: "Stonehange" is not equal to "Stonhenge", but it's near - I'd like to know how near a match the strings are.
I only know isEqual:
. If you type "Stonehenge", you match and will get the right symbol. If text field's text is not equal, you do not get a match and get the wrong symbol.
回答1:
EDIT What you want is to compute Levenshtein Distance.
You may want to check out this question Calculate number of differences between two NSStrings
回答2:
I like this library, StringScore https://github.com/thetron/StringScore
It gives you these useful methods:
- (CGFloat) scoreAgainst:(NSString *)otherString;
- (CGFloat) scoreAgainst:(NSString *)otherString fuzziness:(NSNumber *)fuzziness;
- (CGFloat) scoreAgainst:(NSString *)otherString fuzziness:(NSNumber *)fuzziness options:(NSStringScoreOption)options;
来源:https://stackoverflow.com/questions/14445476/compare-similarity-of-two-nsstrings