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

2022-07-09  本文已影响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;

}

上一篇下一篇

猜你喜欢

热点阅读