本文介绍了在 Objective-C 中查找不区分大小写的另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于 如何在 Objective-C 中检查一个字符串是否包含另一个字符串?

如何检查字符串 (NSString) 是否包含另一个较小的字符串但忽略大小写?

How can I check if a string (NSString) contains another smaller string but with ignoring case?

NSString *string = @"hello bla bla";

我希望是这样的:

NSLog(@"%d",[string containsSubstring:@"BLA"]);

无论如何有什么办法可以找到一个字符串是否包含另一个忽略大小写的字符串?但请不要将两个字符串都转换为大写或小写.

Anyway is there any way to find if a string contains another string with ignore case ? But please do not convert both strings to UpperCase or to LowerCase.

推荐答案

与链接中提供的答案类似,但使用 options.

As similar to the answer provided in the link, but use options.

See - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask in Apple doc

NSString *string = @"hello bla bla";

if ([string rangeOfString:@"BLA" options:NSCaseInsensitiveSearch].location == NSNotFound)
{
    NSLog(@"string does not contain bla");
} 
else 
{
    NSLog(@"string contains bla!");
}

这篇关于在 Objective-C 中查找不区分大小写的另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:31