标签:nspredicate like predicatewithformat 正则
上代码吧,每块代码都可以直接跑,并附有注释和运行结果
//直接使用 == 严格匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"c", @"cd", @"cdd", @"cd", nil]; NSString *match = @"cd"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
//like匹配,类似Sql中的用法 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"d", @"cd", @"cdd", @"dd",@"dcd",@"cdc", nil]; NSString *match = @"c*";//以c开头 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
//忽略大小写进行匹配 NSArray *arrOriginal = [NSArray arrayWithObjects:@"Cdd",@"cDc",@"cdd",@"dcdd", nil]; NSString *match = @"cd*"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[c] %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
//利用正则表达是进行过滤 NSArray *arrOriginal = [NSArray arrayWithObjects:@"c1",@"C3",@"d2",@"c2", nil]; NSString *match = @"^c\\d?"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
//利用过滤器(数组)进行过滤 NSArray *arrFilter = [NSArray arrayWithObjects:@"aa", @"bb", nil]; NSArray *arrOriginal = [NSArray arrayWithObjects:@"dds",@"bb",@"c1",@"C3",@"bb",@"aa",@"c2", nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrFilter];//不包含符合过滤器的内容 NSArray *results = [arrOriginal filteredArrayUsingPredicate:predicate]; NSLog(@"result===%@",results);
标签:nspredicate like predicatewithformat 正则
原文地址:http://blog.csdn.net/u011439689/article/details/41383909