Check that the text of the textbox is almost equal to the string

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

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

      

But I also want to check if the text of the textbox matches "exampletext" if the text is almost the same as "exampletext". For example, if the text was "eampletex", I want NSLog (@ "Close")

Is there a way to check if the text of the textbox matches 50% equal to "exampletext"? Or any ways to check if the text of the textbox 50% has the same characters as "exampletext"? Or something else like that?

+3


source to share


4 answers


Here come here. Create your own character set from the string you want to match. Check each character in texfield.text for 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 are looking for, but maybe it will help anyway ...

+1


source


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



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

+3


source


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

Sort the characters of both strings into arrays. Determine which line you want to be the main line and take the length of the line.

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

Each time a letter is found, add one to the invoice. If by the end of your loop your score is 80% or more of the original length that you grabbed from the main string, you will most likely get a partial match.

So, for our example, Word 1 will be our main line, and its length will be 5. "ello" is 4/5 characters long and therefore matches 80% of the original line.

0


source


I don't think there is an easy way (with a few lines of code) to solve this. There are several algorithms you can consider and choose the one that best suits your needs.

You should look at this question. Although it was designed and answered in a different language, you asked for a way or method so that you have your solution.

0


source







All Articles