Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate that ignores whitespaces

I need to use NSPredicate to match two strings, case-insensitive, diacritic insensitive, and whitespace-insensitive.

The predicate would look something like this:

[NSPredicate predicateWithFormat:@"Key ==[cdw] %@", userInputKey];

The 'w' modifier is an invented one to express what I'd like to use.

I can't just trim the userInputKey because the data-source "Key" values might have whitespaces in them too (they need those whitespaces, I can't just trim them beforehand).

For example, given a userInputKey "abc" the predicate should match all of

{"abc", "a b c", " a B    C   "}
and so on. Given a userInputKey"a B C" the predicate should also match all the values in the set above.

This can't be so hard to do, can it?

like image 644
JiaYow Avatar asked Apr 10 '12 12:04

JiaYow


1 Answers

How about defining something like this:

+ (NSPredicate *)myPredicateWithKey:(NSString *)userInputKey {
    return [NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedString, NSDictionary *bindings) {
        // remove all whitespace from both strings
        NSString *strippedString=[[evaluatedString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
        NSString *strippedKey=[[userInputKey componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
        return [strippedString caseInsensitiveCompare:strippedKey]==NSOrderedSame;
    }];
}

Then use it like this:

NSArray *testArray=[NSArray arrayWithObjects:@"abc", @"a bc", @"A B C", @"AB", @"a B d", @"A     bC", nil];
NSArray *filteredArray=[testArray filteredArrayUsingPredicate:[MyClass myPredicateWithKey:@"a B C"]];               
NSLog(@"filteredArray: %@", filteredArray);

The result is:

2012-04-10 13:32:11.978 Untitled 2[49613:707] filteredArray: (
    abc,
    "a bc",
    "A B C",
    "A     bC"
)
like image 114
Nick Moore Avatar answered Sep 24 '22 14:09

Nick Moore