iOS_NSPredicate的使用
来源:互联网 发布:mac版网络游戏 编辑:程序博客网 时间:2024/06/06 01:58
在使用UISearchBar进行搜索时,我们会根据搜索框输入的内容,对数据进行过滤、提示。这个时候就需要使用NSPredicate,对输入的数据进行过滤。
- NSPredicate初始化
- Format后面的字符串格式要求
- NSPredicate的使用
- NSArray过滤
- 判断字符串首字母是否为字母
- 字符串替换
- 截取字符串
- 验证邮箱电话号码有效性
- NSDate筛选
- NSPredicate初始化
NSPredicate初始化
+(NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...; //eg NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"self contains[c] %@", filterString];
Format后面的字符串格式要求
// 1、比较运算符 >、<、==、>=、<=、!=例:@"number > 100"// 2、范围运算符 IN、BETWEEN例:@"number BETWEEN {1,5}" @"address IN {'shanghai','beijing'}"// 3、字符串本身 SELF例:@“SELF == ‘APPLE’"// 4、字符串相关 BEGINSWITH、ENDSWITH、CONTAINS例:@"name CONTAIN[cd] 'ang'" //包含某个字符串 @"name BEGINSWITH[c] 'sh'" //以某个字符串开头 @"name ENDSWITH[d] 'ang'" //以某个字符串结束注:[c]不区分大小写,[d]不区分发音符号即没有重音符号,[cd]既不区分大小写,也不区分发音符号。// 5、通配符 LIKE例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd]. @"name LIKE[cd] '???er*'"// 6、正则表达式 MATCHES例:NSString *regex = @"^A.+e$"; //以A开头,e结尾 @"name MATCHES %@",regex
NSPredicate的使用
NSArray过滤
NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil]; NSString *string = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);
判断字符串首字母是否为字母
NSString *regex = @"[A-Za-z]+"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; if ([predicate evaluateWithObject:aString]) { }
字符串替换
NSError* error = NULL; NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(encoding=\")[^\"]+(\")" options:0 error:&error]; NSString* sample = @"<xml encoding=\"abc\"></xml><xml encoding=\"def\"></xml><xml encoding=\"ttt\"></xml>"; NSLog(@"Start:%@",sample); NSString* result = [regex stringByReplacingMatchesInString:sample options:0 range:NSMakeRange(0, sample.length) withTemplate:@"$1utf-8$2"]; NSLog(@"Result:%@", result);
截取字符串
//组装一个字符串,需要把里面的网址解析出来 NSString *urlString=@"<meta/><link/><title>1Q84 BOOK1</title></head><body>"; //NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个 NSError *error; //http+:[^\\s]* 这个表达式是检测一个网址的。(?<=title\>).*(?=</title)截取html文章中的<title></title>中内文字的正则表达式 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=title\\>).*(?=</title)" options:0 error:&error]; if (regex != nil) { NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])]; if (firstMatch) { NSRange resultRange = [firstMatch rangeAtIndex:0]; //从urlString当中截取数据 NSString *result=[urlString substringWithRange:resultRange]; //输出结果 NSLog(@"->%@<-",result); } }
验证邮箱、电话号码有效性
//是否是有效的正则表达式+(BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression{ NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", strExpression]; return [predicate evaluateWithObject:strDestination];}//验证email+(BOOL)isValidateEmail:(NSString *)email { NSString *strRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}"; BOOL rt = [CommonTools isValidateRegularExpression:email byExpression:strRegex]; return rt;}//验证电话号码+(BOOL)isValidateTelNumber:(NSString *)number { NSString *strRegex = @"[0-9]{1,20}"; BOOL rt = [CommonTools isValidateRegularExpression:number byExpression:strRegex]; return rt;}
NSDate筛选
//日期在十天之内:NSDate *endDate = [[NSDate date] retain];NSTimeInterval timeInterval= [endDate timeIntervalSinceReferenceDate];timeInterval -=3600*24*10;NSDate *beginDate = [[NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval] retain];//对coredata进行筛选(假设有fetchRequest)NSPredicate *predicate_date =[NSPredicate predicateWithFormat:@"date >= %@ AND date <= %@", beginDate,endDate];[fetchRequest setPredicate:predicate_date];//释放retained的对象[endDate release];[beginDate release];
阅读全文
0 0
- iOS_NSPredicate的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- python实现找到给定列表中满足给定和的所有子列表,元素可重复使用
- CountDownLatch
- Jquery使用代理的方式绑定控件事件
- sqlzoo练习答案--SELECT names/zh
- QT类学习系列(2)
- iOS_NSPredicate的使用
- 著名的Facebook高管因担心机器人接管世界辞职隐居,你难道不怕吗?
- 算法学习笔记--1. Two Sum
- 【面试题】设计模式相关
- C#中String s1 = null;String s2 = “” 的区别
- 信息安全快讯2017年8月第1期
- TypeAdapter介绍[L]
- -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法
- 常用对话框