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

前端之家收集整理的这篇文章主要介绍了NSPredicate使用(4)——字符串比较运算前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. NSString *text = @"devZhang";
  1. // BEGINSWITH:检查某个字符串是否以指定的字符串开头(如判断字符串是否以a开头:BEGINSWITH 'a')
  2. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'd'"];
  3. NSLog(@"%@ beginWith d: %d",text,[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang beginWith d: 1
  1. // ENDSWITH:检查某个字符串是否以指定的字符串结尾
  2. predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH 'a'"];
  3. NSLog(@"%@ endWith a: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang endWith a: 0
  1. // CONTAINS:检查某个字符串是否包含指定的字符串
  2. predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'evZ'"];
  3. NSLog(@"%@ 包含 evZ: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang 包含 evZ: 1
  1. // LIKE:检查某个字符串是否匹配指定的字符串模板。其之后可以跟?代表一个字符和*代表任意多个字符两个通配符。比如"name LIKE '*ac*'",这表示name的值中包含ac则返回YES;"name LIKE '?ac*'",表示name的第2、3个字符为ac时返回YES。
  2. predicate = [NSPredicate predicateWithFormat:@"SELF LIKE '?evZ*g'"];
  3. NSLog(@"%@ like ?evZ*g: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.384 DemoNSPredicate[2472:315182] devZhang like ?evZ*g: 1
  1. // MATCHES:匹配,即检查某个字符串是否匹配指定的正则表达式。虽然正则表达式的执行效率是最低的,但其功能是最强大的,也是我们最常用的。
  2. // 判断字符串是否以d字符开头,g字符结尾
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^d.+g$'"];
  4. NSLog(@"%@ match d开头/g结尾: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] devZhang match d开头/g结尾: 1
  1. // 判断字符串是否为1~20位的字母字符串(没有位数限制时,可写成"[a-zA-Z]{1,}",或"[a-zA-Z]+")
  2. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '[a-zA-Z]{1,20}'"];
  3. NSLog(@"%@ match 1~20位字母: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] devZhang match 1~20位字母: 1
  1. // 判断字符串是否以下划线开头包含数字和字母的6~12位
  2. text = @"_168devZhang";
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^_+[0-9]+[a-zA-Z]{6,12}'"];
  4. NSLog(@"%@ match 下划线开头+数字+字母+6~12位: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] _168devZhang match 下划线开头+数字+字母+6~12位: 1
  1. // 判断字符串长度
  2. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '([a-zA-Z0-9._&*]{8,16})'"];
  3. NSLog(@"%@ match 8~16位: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.385 DemoNSPredicate[2472:315182] _168devZhang match 8~16位: 1
  1. // 判断数字带两位小数
  2. text = @"123.22";
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^[0-9]+(.[0-9]{2})'"];
  4. NSLog(@"%@ match 数字带两位小数: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 123.22 match 数字带两位小数: 1
  1.  
  1. /**
  2. * 手机号码
  3. * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  4. * 联通:130,131,132,152,155,156,185,186
  5. * 电信:133,1349,153,180,189
  6. */
  7. text = @"13511113244";
  8. 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}'"];
  9. NSLog(@"%@ match 手机号码: %d",[predicate evaluateWithObject:text]);
  10.  
  11. 2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13511113244 match 手机号码: 1
  1. // 中国移动:134[0-8],188
  2. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(34[0-8]|(3[5-9]|5[017-9]|8[278]))[0-9]{8}$'"];
  3. NSLog(@"%@ match 中国移动手机号码: %d",[predicate evaluateWithObject:text]);
  4.  
  5. 2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13511113244 match 中国移动手机号码: 1
  1. // 中国联通:130,186
  2. text = @"18555551155";
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(3[0-2]|5[256]|8[56])[0-9]{8}$'"];
  4. NSLog(@"%@ match 中国联通手机号码: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 18555551155 match 中国联通手机号码: 1

  1. // 中国电信:133,189
  2. text = @"13498161155";
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^1(33|53|8[09])[0-9]{8}|^1(349)[0-9]{7}'"];
  4. NSLog(@"%@ match 中国电信手机号码: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.386 DemoNSPredicate[2472:315182] 13498161155 match 中国电信手机号码: 1
  1. /**
  2. * 大陆地区固话及小灵通
  3. * 区号:010,020,021,022,023,024,025,027,028,029,0755,0753,……
  4. * 号码:七位或八位
  5. */
  6. text = @"075522222222";
  7. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '^0(10|2[05789])[1-9]{8}|^0[0-9]{3}[0-9]{7,8}'"];
  8. NSLog(@"%@ match 固定电话: %d",[predicate evaluateWithObject:text]);
  9.  
  10. 2018-03-05 15:34:34.387 DemoNSPredicate[2472:315182] 075522222222 match 固定电话: 1
  1. // 验证email
  2. text = @"zhangsy@163.com";
  3. predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES '[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}'"];
  4. NSLog(@"%@ match 电子邮箱: %d",[predicate evaluateWithObject:text]);
  5.  
  6. 2018-03-05 15:34:34.387 DemoNSPredicate[2472:315182] zhangsy@163.com match 电子邮箱: 1
@H_403_36@

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