//第一种方式是使用NSScanner:
//1. 整形判断
- (BOOL)isPureInt:(NSString *)string{
NSScanner* scan = [NSScanner scannerWithString:string];
intval;
return [scan scanInt:&val] &&[scan isAtEnd];
}
//2.浮点形判断:
- (BOOL)isPureFloat:(NSString *)string{
NSScanner* scan = [NSScanner scannerWithString:string];
floatval;
return [scan scanFloat:&val] &&[scan isAtEnd];
}
//(利用1. 整形判断 2.浮点形判断 )判断是否是纯数字
+(BOOL)stringIsNumbers:(NSString *)string{
if( ![Tools isPureInt:string] && ![Tools isPureFloat:string]){
returnNO;
}else{
returnYES;
}
}
//第二种方式是使用循环判断
- (BOOL)isPureNumandCharacters:(NSString *)text
{for(inti =0; i < [text length]; ++i) {
int a =[text characterAtIndex:i];
if([self isNum:a]){
continue;
}else{
returnNO;
}
}returnYES;
}
//C语言中常用的方式.
- (BOOL)isAllNum:(NSString *)string{
unichar c;
for(int i=0; i < string.length; i++) {
c=[string characterAtIndex:i];
if(!isdigit(c)) {
returnNO;
}
}returnYES;
}
//第三种方式则是使用NSString的trimming方法
- (BOOL)isPureNumandCharacters:(NSString *)string{
string= [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
if(string.length >0){
returnNO;
}
returnYES;
}//用正则表达式判断
- (BOOL) deptNumInputShouldNumber:(NSString *)str
{
NSString*regex =@"[0-9]*";
NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if([pred evaluateWithObject:str]) {
return YES;
}
return NO;
}
网友评论