iOS--判斷字串NSString中數字、中文、大小寫英文
阿新 • • 發佈:2019-02-11
NSString *testString = @"中文123ljfLJF";
NSInteger alength = [testString length];
for (int i = 0; i<alength; i++) {
char commitChar = [testString characterAtIndex:i];
NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
const char *u8Temp = [temp UTF8String];
if (3==strlen(u8Temp)){
NSLog(@"字串中含有中文");
}else if((commitChar>64)&&(commitChar<91)){
NSLog(@"字串中含有大寫英文字母");
}else if((commitChar>96)&&(commitChar<123)){
NSLog(@"字串中含有小寫英文字母");
}else if((commitChar>47)&&(commitChar<58 )){
NSLog(@"字串中含有數字");
}else{
NSLog(@"字串中含有非法字元");
}
}
//判斷是否為整形:
- (BOOL)isPureInt:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
int val;
return[scan scanInt:&val] && [scan isAtEnd];
}
//判斷是否為浮點形:
- (BOOL )isPureFloat:(NSString*)string{
NSScanner* scan = [NSScanner scannerWithString:string];
float val;
return[scan scanFloat:&val] && [scan isAtEnd];
}
if( ![self isPureInt:textField.text] || ![self isPureFloat:textField.text]){
textField.textColor = [UIColor redColor];
textField.text = @"警告:含非法字元,請輸入純數字!";
return;
}else{
NSLog(@"整形或浮點型");
}