//直接使用 == 严格匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"c",@"cd",@"cdd",nil]; NSString *match = @"cd"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@",match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
打印结果:
result===(
cd,
cd
)
//like匹配,类似sql中的用法 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"d",@"dd",@"dcd",@"cdc",nil]; NSString *match = @"c*";//以c开头 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@",results);
打印结果:
result===(
c1,
cd,
cdd,
cdc
)
//忽略大小写进行匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"Cdd",@"cDc",@"dcdd",nil]; NSString *match = @"cd*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@",results);
打印结果:
result===(
Cdd,
cDc,
cdd
)
//利用正则表达是进行过滤 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"C3",@"d2",@"c2",nil]; NSString *match = @"^c\\d?"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@",
c2
)//利用过滤器(数组)进行过滤 NSArray *arrFilter = [NSArray arrayWithObjects:@"aa",@"bb",nil]; NSArray *arrOriginal = [NSArray arrayWithObjects:@"dds",@"c1",@"aa",nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)",arrFilter];//不包含符合过滤器的内容 NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);打印结果: result===( dds,c1,C3,c2 )