Objective-C用于判断字符串的类型
#pragma mark - 判断全汉字
+ (BOOL)inputShouldChinese:(NSString *)inputString {
if (inputString.length == 0) return NO;
NSString *regex = @"[\u4e00-\u9fa5]+";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
#pragma mark - 判断全数字
+ (BOOL)inputShouldNumber:(NSString *)inputString {
if (inputString.length == 0) return NO;
NSString *regex = @"[0-9]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
#pragma mark - 判断小数
+ (BOOL)inputShouldDecimal:(NSString *)inputString {
if (inputString.length == 0) return NO;
NSString *regex = @"^[0-9]+(\\.[0-9]{1,2})?$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
#pragma mark - 判断全字母
+ (BOOL)inputShouldLetter:(NSString *)inputString {
if (inputString.length == 0) return NO;
NSString *regex = @"[a-zA-Z]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}
#pragma mark - 判断字母或者数字
+ (BOOL)inputShouldLetterOrNum:(NSString *)inputString {
if (inputString.length == 0) return NO;
NSString *regex = @"[a-zA-Z0-9]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [pred evaluateWithObject:inputString];
}