iOS--判断字符串NSString中数字、中文、大小写英文,有需要的朋友可以参考下。



 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(@"整形或浮点型");
            }

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2021-09-25
相关资源
相似解决方案