前端之家收集整理的这篇文章主要介绍了
判断字符串是否包含某些字符,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一、判断字符串是否包含某一个字符
NSString *str1 = @"abcd";
NSString *str = @"a";
if ([str1 rangeOfString:str].location != NSNotFound) {
NSLog(@"这个字符串中有a");
}
二、判断字符串中是否包含数字
#pragma mark - 判断字符串是否包含数字
- (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)];
if (count > 0) {
return YES;
}
return NO;
}
三、判断字符串中是否包含英文字母
#pragma mark - 判断字符串是否包含数字
- (BOOL)isStringContainNumberWith:(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)];
if (count > 0) {
return YES;
}
return NO;
}
四、判断字符串中是否包含数字和字母
#pragma mark - 判断字符串是否包含数字
- (BOOL)isStringContainNumberWith:(NSString *)str {
NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"A-Za-z0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0,str.length)];
if (count > 0) {
return YES;
}
return NO;
}