NSPredicate忽略空格 [英] NSPredicate that ignores whitespaces

查看:132
本文介绍了NSPredicate忽略空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用NSPredicate来匹配两个字符串,不区分大小写,区分不区分大小写,和空白不区分大小写



看起来像这样:

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

'w'修饰符是一个用来表达我想使用的修饰符。 p>

我不能修剪 userInputKey ,因为数据源Key值可能也有空格



例如,给定一个 userInputKey abc谓词应该匹配

 {abc,abc,a BC} 

等等。给定 userInputKey a BC,谓词也应该匹配上面集合中的所有值。



解决方案

如何定义这样的东西:

  +(NSPredicate *)myPredicateWithKey:(NSString *)userInputKey {
return [NSPredicate predicateWithBlock:^ BOOL(NSString * calculatedString,NSDictionary * bindings) {
//从两个字符串中删除所有空格
NSString * strippedString = [[evaluateString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@];
NSString * strippedKey = [[userInputKey componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@];
return [strippedString caseInsensitiveCompare:strippedKey] == NSOrderedSame;
}];
}

然后使用它:

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

结果是:

  2012-04-10 13:32:11.978 Untitled 2 [49613:707] filteredArray:(
abc,
a bc,
ABC
A bC


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?

解决方案

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"
)

这篇关于NSPredicate忽略空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆