判断手机所属运营商
//判断手机所属运营商
+(NSString *)checkMobilePhoneNumberBelongsToOperators:(NSString *)phoneNumber{
/**
* 三大运营商最新号段 合作版
移动号段:
134 135 136 137 138 139 147 150 151 152 157 158 159 178 182 183 184 187 188
^1(3[4-9]|47|5[0-2]|5[7-9]|78|8[2-4]|8[78])\\d{8}
联通号段:
130 131 132 145 155 156 171 175 176 185 186
^1(3[012]|45|5[56]|7[156]|8[56])\\d{8}
电信号段:
133 149 153 173 177 180 181 189
^1(33|49|53|7[37]|8[019])\\d{8}
虚拟运营商:
170
^170\\d{8}$
*/
//这里查找手机号中有没有+86这种前缀,如果有的话就给他截掉,让他变为正常的手机串号
NSRange range = [phoneNumber rangeOfString:@"+86" options:NSBackwardsSearch];
if (range.location != NSNotFound) {
NSRange rangeNew;
rangeNew.location = range.length;
rangeNew.length = phoneNumber.length - range.length;
phoneNumber = [phoneNumber substringWithRange:rangeNew];
}
NSString *pattern1 = @"^1(3[4-9]|47|5[0-2]|5[7-9]|78|8[2-4]|8[78])\\d{8}";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern1];
if ([pred1 evaluateWithObject:phoneNumber]) {
return @"中国移动";
}
NSString *pattern2 = @"^1(3[012]|45|5[56]|7[156]|8[56])\\d{8}";
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern2];
if ([pred2 evaluateWithObject:phoneNumber]) {
return @"中国联通";
}
NSString *pattern3 = @"^1(33|49|53|7[37]|8[019])\\d{8}";
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern3];
if ([pred3 evaluateWithObject:phoneNumber]) {
return @"中国电信";
}
NSString *pattern4 = @"^170\\d{8}";
NSPredicate *pred4 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern4];
if ([pred4 evaluateWithObject:phoneNumber]) {
return @"虚拟运营商";
}
return @"未知运营商";
}
判断手机号是否正确
//匹配手机号
+(BOOL)checkUserTelNumber:(NSString *)userTelNumber{
NSString *pattern = @"^1+[34578]+\\d{9}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
return [pred evaluateWithObject:userTelNumber];
}
判断用户身份证号码15位或者18位
//匹配用户身份证号码15位或者18位
+(BOOL)checkUserIDCard:(NSString *)userIDCard{
NSString *pattern = @"^([0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
// NSString *pattern = @"^(\\d{15}$)|(\\d{17}+(\\d|X)$)";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
return [pred evaluateWithObject:userIDCard];
}
判断用户姓名,20位的中文或英文
//匹配用户姓名,20位的中文或英文
+(BOOL)checkUserName:(NSString *)userName{
NSString *pattern = @"^([a-zA-Z]{1,20})|([\u4E00-\u9fa5]|·){1,20}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
return [pred evaluateWithObject:userName];
}
检测邮箱是否正确
//检测邮箱是否正确
+(BOOL)checkEmail:(NSString *)email{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
过滤字符串中间和两边的空格
//过滤字符串中间和两边的空格
+(NSString *)FilterSpaceString:(NSString *)string{
NSString *newStr;
//过滤掉字符串前后的空格
newStr = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//过滤掉中间的空格
newStr = [newStr stringByReplacingOccurrencesOfString:@" " withString:@""];
return newStr;
}
隐藏掉姓名的第一个字
//显示最后一个字 剩下*
+ (NSString*)PersonNameSecret:(NSString*)name{
for (int i = 0 ; i < name.length - 1;i ++) {
//显示最后一个字
// name = [name stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
//隐藏第一个字
name = [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"*"];
}
return name;
}
去掉小数点后面多余的0
//浮点数处理并去掉多余的0
+(NSString *)stringDisposeWithFloat:(NSString *)floatValue
{
NSString *str = [NSString stringWithFormat:@"%@",floatValue];
long len = str.length;
for (int i = 0; i < len; i++)
{
if (![str hasSuffix:@"0"])
break;
else
str = [str substringToIndex:[str length]-1];
}
if ([str hasSuffix:@"."])//避免像2.0000这样的被解析成2.
{
return [str substringToIndex:[str length]-1];//s.substring(0, len - i - 1);
}
else
{
return str;
}
}
截取MM名称后的 “XXXX”
//截取MM名称后的 “XXXX”
+(NSString *)subBankNameString:(NSString *)bankName{
NSRange range = [bankName rangeOfString:@"MM" options:NSBackwardsSearch];
if (range.location != NSNotFound) {
// NSLog(@"range = %d",range.location);
NSRange rangeNew;
rangeNew.location = 0;
rangeNew.length = range.location + 2;
bankName = [bankName substringWithRange:rangeNew];
}
return bankName;
}
16进制颜色(html颜色值)字符串转为UIColor
//16进制颜色(html颜色值)字符串转为UIColor
+ (UIColor *) hexStringToColor: (NSString *) stringToConvert
{
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor blackColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
if ([cString length] != 6) return [UIColor blackColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
隐藏银行卡号中间的数字
//隐藏银行卡号中间的数字,前4后6 剩下*
+ (NSString*)bankCardNumSecret:(NSString*)cardNum{
for (int i = 6 ; i < cardNum.length - 4;i ++) {
cardNum = [cardNum stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
}
return cardNum;
}
网友评论