平常判斷數(shù)組中是否包含某個(gè)字符我們通常使用containsObject
,但這種方式是嚴(yán)格區(qū)分大小寫的溜腐。
當(dāng)我們需要不區(qū)分大小寫來(lái)判斷包含時(shí)可以使用indexOfObjectPassingTest
配合caseInsensitiveCompare
來(lái)實(shí)現(xiàn)
當(dāng)我們判斷單個(gè)字符并不區(qū)分大小寫時(shí)掸哑,通常使用caseInsensitiveCompare
[str caseInsensitiveCompare:@"abc"] == NSOrderedSame
所以當(dāng)我們需要判斷數(shù)組是否包含時(shí)跌造,可以這樣寫
//返回?cái)?shù)組中是否包含字符 不區(qū)分大小寫
- (BOOL)caseInsensitiveCompareWithArray:(NSArray *)array string:(NSString *)string{
return ([array indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return (BOOL)([obj caseInsensitiveCompare:string] == NSOrderedSame);
}] != NSNotFound);
}
使用:
if ([self caseInsensitiveCompareWithArray:arr string:str]){
//arr中包含str征炼,不區(qū)分大小寫
}