Reputation: 87
Does anyone now how I can check if a text field NEARLY matches a set text?
I know how to check if it exactly matches, but i want it to know if its even close to the set text
So if they type HELLO WORD it indicates its close but not exact match?
if (([textfield.text isEqual:@"HELLO WORLD"]))
{
NSLog(@"Correct");
} else {
NSLog(@"Incorrect");
}
Upvotes: 0
Views: 255
Reputation: 1239
NSString *string = @"HELLO WORLD I AM JACK";
if ([string rangeOfString:@"HELLO WORLD"].location == NSNotFound) {
NSLog(@"string does not contain HELLO WORLD");
} else {
NSLog(@"string contains HELLO WORLD!");
}
Upvotes: 0
Reputation: 5175
use
NSString caseInsensitiveCompare:
or
- (NSComparisonResult)compare:(NSString *)aString
options:(NSStringCompareOptions)mask`
Upvotes: 0
Reputation: 4244
Use this
For Case Insensitive :
if( [textfield.text caseInsensitiveCompare:@"My Case sensitiVE"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
For Case Sensitive :
if([textfield.text isEqualToString:@"My Case sensitiVE"]) {
// Case sensitive Compare
}
Upvotes: 1
Reputation: 11197
You can compare each index of two string and see how many difference is there. And you should define your "nearly match", it may be difference in single character or in multiple character. And decide if you should accept it or reject it.
If you like algorithm Longest Common Subsequence is a key to your goal.. :)
Upvotes: 0
Reputation: 2942
This library may be of use to you. And since it's open source, you can check the source to see how it's done. :)
Upvotes: 1