Objective-C NSString 方法需要检查一个字符串是否包含在另一个字符串中?

What Objective-C NSString method need to check if a string is contained in another?

NSString 的什么方法检查 localizedCaseInsensitiveCompare: keyword 找到的字符串是否包含在另一个中?

NSString *listOfNames = @"anas, ward, qusai, zainab";
NSString *keyword = @"Ward";

if ([listOfNames localizedCaseInsensitiveCompare:keyword]) {
    NSLog(@"\nMatch found!\n");
} else {
    NSLog(@"\nNo match found!\n");
}

localizedCaseInsensitiveCompare 很可能在内部将两个字符串都转换为小写,然后使用本地化排序算法进行比较。

来自文档: "Localized string comparisons are based on the Unicode Collation Algorithm, as tailored for different languages by CLDR (Common Locale Data Repository). Both are projects of the Unicode Consortium. Unicode is a registered trademark of Unicode, Inc."

可能合理的做法是将每个字符串转换为小写,使用 listOfNames.lowercaseString

我假设您的意思是您正在使用 localizedCaseInsensitiveContainsString:,因为您引用的方法与您所说的不符。

有一个相应的方法来获取搜索词的范围:-[NSString localizedStandardRangeOfString:]您使用返回的范围来索引回源字符串。

如果您需要使用当前区域以外的区域进行搜索,您也可以使用 rangeOfString:options:range:locale: