如何用NSPredicate过滤数组来组合单词

[filteredArray filterUsingPredicate: [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@", searchText]]; 

filteredArray包含简单的NSString。 [你好,我的,得到,七,等等];

它会给所有以searchText开头的string。

但是,如果string将是“我的名字是”和searchText = name的单词的组合。 NSPredicate将如何实现这一目标?

更新:如何,如果我想结果searchText = name ,但不searchText = ame ? 也许这样:

  [filteredArray filterUsingPredicate: [NSPredicate predicateWithFormat: @"self BEGINSWITH[cd] %@ or self CONTENTS[cd] %@", searchText, searchText]]; 

但是,它应该首先显示以searchText开始的string,并且仅在包含searchText的string之后显示。

 [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@", searchText]; 

问题扩展后编辑

 NSArray *beginMatch = [filteredArray filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"self BEGINSWITH[cd] %@", searchText]]; NSArray *anyMatch = [filteredArray filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"self CONTAINS[cd] %@", searchText]]; NSMutableArray *allResults = [NSMutableArray arrayWithArray:beginMatch]; for (id obj in anyMatch) { if (![allResults containsObject:obj]) { [allResults addObject:obj]; } } filteredArray = allResults; 

这将以所需的顺序得到结果,而不会有重复的条目。

编辑其实开始从string开始检查searchstring的长度。 如果发现完全匹配,那么它的过滤

 if u have name game tame lame search Text : ame filtered text would be: none 

也包含从string开头到searchstring长度的检查,但如果find开始,中间或结束精确searchstring,则它被过滤。

 if u have name game tame lame search Text : ame filtered text would be: name game tame lame because all has ame 

 [NSPredicate predicateWithFormat:@"self CONTAINS '%@'", searchText];