在 Objective-C 中查找一个不区分大小写的字符串
我的问题类似于 How do I check if a string contains another string in Objective-C?
如何检查string (NSString) 包含另一个较小的字符串但忽略大小写?
NSString *string = @"hello bla bla";
我希望得到类似的结果:
NSLog(@"%d",[string containsSubstring:@"BLA"]);
无论如何,有什么方法可以查找一个字符串是否包含另一个忽略大小写的字符串?但请不要将这两个字符串都转换为大写或小写。
My question is similar to How do I check if a string contains another string in Objective-C?
How can I check if a string (NSString) contains another smaller string but with ignoring case?
NSString *string = @"hello bla bla";
I was hoping for something like:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与链接中提供的答案类似,但使用
options
。请参阅 Apple 文档
As similar to the answer provided in the link, but use
options
.See
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
in Apple doc从 iOS 8 开始,您可以将
containsString:
或localizedCaseInsensitiveContainsString
方法添加到 NSString。From iOS 8 you can add the
containsString:
orlocalizedCaseInsensitiveContainsString
method to NSString.方法
应该对你有帮助。
The method
should help you.
您可以使用
-(NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
来获取子字符串的范围,mask
参数用于指定不区分大小写的匹配。示例:
如 文档,该方法返回一个范围,如
{NSNotFound, 0}
当未找到子字符串时。重要如果字符串为
nil
,此方法会引发异常。You can use
-(NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
to get a range for a substring, themask
parameter is used to specify case insensitive match.Example :
As stated in the documentation, the method returns a range like
{NSNotFound, 0}
when the substring isn't found.Important this method raises an exception if the string is
nil
.对于 Swift 4:
用法:
输出:
For Swift 4:
Usage:
Output: