美文网首页
判断字符串中是否包含字母和数字

判断字符串中是否包含字母和数字

作者: 见路_不走 | 来源:发表于2022-07-09 13:57 被阅读0次

判断字符串中是否包含字母和数字

- (BOOL)isContainA_z_0_9WithString:(NSString *)strtext{
    if ([self isStringContainNumberWith:strtext] && [self isStringContainA_ZWith:strtext] ) {
        return YES;
    }
    return NO;
}

判断字符串中是否包含字母
- (BOOL)isStringContainA_ZWith:(NSString *)str {

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

NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];

//count是str中包含[A-Za-z]数字的个数,只要count>0,说明str中包含数字

if (count > 0) {

return YES;

}

return NO;

}

判断字符串中是否包含数字

- (BOOL)isStringContainNumberWith:(NSString *)str {

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

NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];

//count是str中包含[0-9]数字的个数,只要count>0,说明str中包含数字

if (count > 0) {

return YES;

}

return NO;

}

相关文章

网友评论

      本文标题:判断字符串中是否包含字母和数字

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