2014-01-08 32 views
0

我必须从使用NSPredicate (CoreData)的字符串列表中获取特定的字符串。为例如: 如果我有从CoreData如何从字符串列表中找到特定的字符串?

  1. 从上面的列表以下数组的Helloworld

  2. helloworld1234

  3. 的HelloWorld 12345

我只需要1和3结果一个。 我曾尝试以下,但我得到的所有三个作为结果

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"contactName CONTAINS [c]%@",[arySeperator objectAtIndex:1]]; 
NSArray *filteredArray1 = [arrayContacts filteredArrayUsingPredicate:predicate]; 

在哪里,我错了?

回答

0

LIKE 左手表达式等于右手表达式:?和*允许作为通配符,在哪里?匹配1个字符并*匹配0个或更多字符。

NSString *textSearch = @"Raja"; 
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"firstname LIKE '*%[email protected]*' OR lastname LIKE '*%[email protected]*'", textSearch]; 
1

查找包含给定文本为“全字”的所有条目,你可以使用 谓词“匹配”操作,这对正则表达式匹配,而 字边界模式\b

NSString *searchWord = @"Helloworld"; 
NSString *pattern = [NSString stringWithFormat:@".*\\b%@\\b.*", searchWord]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"contactName MATCHES[c] %@", pattern]; 

如果这个谓词添加到核心数据读取请求这也应该工作, 而不是过滤管理对象的获取已经阵列。

0

您可以使用下面的代码来获得一个字符串是否是存在于一个NSArray:

NSArray* yourArray = [NSArray arrayWithObjects: @"Str1", @"Str2", @"Str3", nil]; 
if ([yourArray containsObject: yourStringToFind]) { 
    // do found 
} else { 
    // do not found 
} 
相关问题