NSPredicate使用(4)——字符串比较运算

前端之家收集整理的这篇文章主要介绍了NSPredicate使用(4)——字符串比较运算前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
NSString *text = @"devZhang";
// BEGINSWITH:检查某个字符串是否以指定的字符串开头(如判断字符串是否以a开头:BEGINSWITH 'a')
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'd'"];
NSLog(@"%@ beginWith d: %d",text,[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang beginWith d: 1
// ENDSWITH:检查某个字符串是否以指定的字符串结尾
predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH 'a'"];
NSLog(@"%@ endWith a: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang endWith a: 0
// CONTAINS:检查某个字符串是否包含指定的字符串
predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'evZ'"];
NSLog(@"%@ 包含 evZ: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang 包含 evZ: 1
// LIKE:检查某个字符串是否匹配指定的字符串模板。其之后可以跟?代表一个字符和*代表任意多个字符两个通配符。比如"name LIKE '*ac*'",这表示name的值中包含ac则返回YES;"name LIKE '?ac*'",表示name的第2、3个字符为ac时返回YES。
predicate = [NSPredicate predicateWithFormat:@"SELF LIKE '?evZ*g'"];
NSLog(@"%@ like ?evZ*g: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang like ?evZ*g: 1
// MATCHES:匹配,即检查某个字符串是否匹配指定的正则表达式。虽然正则表达式的执行效率是最低的,但其功能是最强大的,也是我们最常用的。
// 判断字符串是否以d字符开头,g字符结尾
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^d.+g$'"];
NSLog(@"%@ match d开头/g结尾: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] devZhang match d开头/g结尾: 1
// 判断字符串是否为1~20位的字母字符串(没有位数限制时,可写成"[a-zA-Z]{1,}",或"[a-zA-Z]+")
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '[a-zA-Z]{1,20}'"];
NSLog(@"%@ match 1~20位字母: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] devZhang match 1~20位字母: 1
// 判断字符串是否以下划线开头包含数字和字母的6~12位
text = @"_168devZhang";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^_+[0-9]+[a-zA-Z]{6,12}'"];
NSLog(@"%@ match 下划线开头+数字+字母+6~12位: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] _168devZhang match 下划线开头+数字+字母+6~12位: 1
// 判断字符串长度
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '([a-zA-Z0-9._&*]{8,16})'"];
NSLog(@"%@ match 8~16位: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] _168devZhang match 8~16位: 1
// 判断数字带两位小数
text = @"123.22";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^[0-9]+(.[0-9]{2})'"];
NSLog(@"%@ match 数字带两位小数: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 123.22 match 数字带两位小数: 1
 
 
/**
     * 手机号码
     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通:130,131,132,152,155,156,185,186
     * 电信:133,1349,153,180,189
     */
text = @"13511113244";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(34[0-8]|(3[5-9]|5[017-9]|8[278]))[0-9]{8}$|^1(3[0-2]|5[256]|8[56])[0-9]{8}$|^1(33|53|8[09])[0-9]{8}|^1(349)[0-9]{7}'"];
NSLog(@"%@ match 手机号码: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13511113244 match 手机号码: 1
// 中国移动:134[0-8],188
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(34[0-8]|(3[5-9]|5[017-9]|8[278]))[0-9]{8}$'"];
NSLog(@"%@ match 中国移动手机号码: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13511113244 match 中国移动手机号码: 1
// 中国联通:130,186
text = @"18555551155";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(3[0-2]|5[256]|8[56])[0-9]{8}$'"];
NSLog(@"%@ match 中国联通手机号码: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 18555551155 match 中国联通手机号码: 1

// 中国电信:133,189
text = @"13498161155";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(33|53|8[09])[0-9]{8}|^1(349)[0-9]{7}'"];
NSLog(@"%@ match 中国电信手机号码: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13498161155 match 中国电信手机号码: 1
/**
     * 大陆地区固话及小灵通
     * 区号:010,020,021,022,023,024,025,027,028,029,0755,0753,……
     * 号码:七位或八位
     */
text = @"075522222222";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^0(10|2[05789])[1-9]{8}|^0[0-9]{3}[0-9]{7,8}'"];
NSLog(@"%@ match 固定电话: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.387 DemoNSPredicate[2472:315182] 075522222222 match 固定电话: 1
// 验证email
text = @"zhangsy@163.com";
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}'"];
NSLog(@"%@ match 电子邮箱: %d",[predicate evaluateWithObject:text]);

2018-03-05 15:34:34.387 DemoNSPredicate[2472:315182] zhangsy@163.com match 电子邮箱: 1

猜你在找的正则表达式相关文章