iOS

iOS检测数字、字母、中文字符的个数

2017-06-22  本文已影响345人  悠辰清泪

- (void)judgeNum:(NSString*)Str {

NSRegularExpression*tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]"options:NSRegularExpressionCaseInsensitive error:nil];

//符合数字条件的有几个字节

NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:Str

options:NSMatchingReportProgress

range:NSMakeRange(0, Str.length)]

//英文字条件

NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];

NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:Str options:NSMatchingReportProgress range:NSMakeRange(0, Str.length)];

//中文条件

NSRegularExpression *tChineseRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[\u4e00-\u9fa5]"options:NSRegularExpressionCaseInsensitive error:nil];

NSUInteger tChineseMatchCount = [tChineseRegularExpression numberOfMatchesInString:Str options:NSMatchingReportProgress range:NSMakeRange(0, Str.length)];

NSLog(@"数字的个数=%ld 字母的个数=%ld汉字的个数=%ld",tNumMatchCount,tLetterMatchCount,tChineseMatchCount);

}

上一篇 下一篇

猜你喜欢

热点阅读