Peter
Peter

Reputation: 1878

Check if textfield text is almost equal to string

I have a UITextField called textfield. And I have this code to check if the text in the textfield is equal to "exampletext"

if ([textfield.text isEqualToString:@"exampletext"]) {
    NSLog(@"Correct");
} else {
    NSLog(@"Wrong");
}

But I also want to check if the text in the textfield is almost equal to "exampletext", if the text is almost the same as "exampletext". Like if the text was "eampletex" I want to NSLog(@"Close")

Are there any ways to check if the textfield text is like 50% equal to "exampletext"? Or any ways to check if the textfield text has 50% the same characters as "exampletext"? Or something else like that?

Upvotes: 3

Views: 1192

Answers (4)

jhilgert00
jhilgert00

Reputation: 5489

Here's my go at it. Create a custom character set from the string you want to match. Check each character in the texfield.text against that character set, and if the number of matches is close to the number of letters in the string, do something..

NSString *testString = @"wordToCompare";
NSString *textFromTextfield = textfield.text;

//create a custom character set from the word you want to compare to...
NSCharacterSet *characterSetForString = [NSCharacterSet characterSetWithCharactersInString:testString];

//keep track of how many matches...
int numberOfCharsThatMatchSet = 0;

    for (int x = 0; x < [textFromTextField length]; x++) {

        unichar charToCheck = [textFromTextField characterAtIndex:x];

        if ([characterSetForString characterIsMember:charToCheck] == YES) {
            numberOfCharsThatMatchSet++;
        }

        NSLog(@"%d", numberOfCharsThatMatchSet);
    }

         // if the number of matches is the same as the length of the word + or - 2...
    if ((numberOfCharsThatMatchSet > [testString length] - 2 ) && (numberOfCharsThatMatchSet < [testString length] + 2 )) {
        NSLog(@"close match...");
    }

Not sure if this is 100% what you're looking for, but maybe it will help anyway...

Upvotes: 1

Bartu
Bartu

Reputation: 2210

I don't think there is an easy way (with several lines of code) of solving this. There are several algorithms you might consider and pick the one which suits your needs most.

You should look at this question. Although it has been designed and answered for another language, you asked for a way or method so you have your solution there.

Upvotes: 0

Roberto
Roberto

Reputation: 2185

What you are looking for is an implementation of the levenshtein distance, levenshtein("hello", "hallo") => 1, levenshtein("hello", "ellos") => 2. You can check this library.

Once you have the distance between the two strings, you could get it as a percentage calculating: percentage = 100 * levenshtein(original,other) / length(original)

Upvotes: 3

Max
Max

Reputation: 6229

I'm sure there might be some open source out there somewhere that would do this for you..however, one approach I can think of that will give you a bit of a lead...

Sort out the characters of both your strings into arrays. Determine which string you want to be the master string and grab the string length of it.

Now compare each character. Ex: Word 1: hello, Word 2: ello.

Each time a letter is found add one to a count. If by the end of your looping your count is 80% of the original length you grabbed from the master string or greater then you most likely have a partial match.

So for our example Word 1 will be our master string and its length is 5. "ello" contains 4/5 characters and therefore is matches 80% of the original string.

Upvotes: 0

Related Questions