一、判断字符串是否包含某一个字符
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)];
//count是str中包含[0-9]数字的个数,只要count>0,说明str中包含数字
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)];
//count是str中包含[A-Za-z]数字的个数,只要count>0,说明str中包含数字
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)];
//count是str中包含[A-Za-z0-9]数字的个数,只要count>0,说明str中包含数字
if (count > 0) {
return YES;
}
return NO;
}
截图中 还有替换字符串内容的方法