美文网首页iOS
iOS检测数字、字母、中文字符的个数

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

作者: 悠辰清泪 | 来源:发表于2017-06-22 10:05 被阅读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);

}

相关文章

网友评论

    本文标题:iOS检测数字、字母、中文字符的个数

    本文链接:https://www.haomeiwen.com/subject/mxegdttx.html