使用NSPredicate进行字符串比较

时间:2014-03-18 07:22:05

标签: ios iphone objective-c nspredicate

我的aryContacts包含test_tech,test,abc_tech,testing,hh_techvedika

 NSArray *arrSeperated = [[[aryContacts objectAtIndex:i]valueForKey:@"contactName"]componentsSeparatedByString:@“_”];
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"contactName CONTAINS [cd]%@",[arrSeperated objectAtIndex:0]];
         NSArray    *arrContacts1 = [aryContacts filteredArrayUsingPredicate:predicate];

    NSPredicate *predicate1 =[NSPredicate predicateWithFormat:@"contactName CONTAINS [cd]%@",[arrSeperated objectAtIndex:1]];
            NSArray    *arrContacts2 = [aryContacts filteredArrayUsingPredicate:predicate1];

结果:arrContacts1包含test_tech,test,testing 预期:arrContacts1包含test_tech,test

结果:arrContacts2包含test_tech,abc_tech,hh_techvedika 预期:arrContacts2包含test_tech,abc_tech

1 个答案:

答案 0 :(得分:1)

尝试以下代码

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"contactName BEGINSWITH [cd]%@", [[arrTempContacts objectAtIndex:i] valueForKey:@"contactName"]];
NSArray *arrContacts = [arrTempContacts filteredArrayUsingPredicate:predicate];  

让我知道它是否有效!!!!

快乐的编码!!!