本文介绍了搜索 NSString 是否包含值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些由几个字符构成的字符串值,我想检查它们是否存在于另一个 NSString 中,不区分大小写和空格.

I have some string value which constructed from a few characters , and i want to check if they exist in another NSString, without case sensitive, and spaces .

示例代码:

NSString *me = @"toBe" ;
NSString *target=@"abcdetoBe" ;
//than check if me is in target.

在这里我会得到 true 因为 me 存在于 target 中.我如何检查这种情况?

Here i will get true because me exist in target .How can i check for such condition ?

我已阅读我如何检查一个字符串是否包含 Objective-C 中的另一个字符串? 但它区分大小写,我需要找到不区分大小写的..

I have read How do I check if a string contains another string in Objective-C? but its case sensitive and i need to find with no case sensitive..

推荐答案

使用选项 NSCaseInsensitiveSearchrangeOfString:options:

NSString *me = @"toBe" ;
NSString *target = @"abcdetobe" ;
NSRange range = [target  rangeOfString: me options: NSCaseInsensitiveSearch];
NSLog(@"found: %@", (range.location != NSNotFound) ? @"Yes" : @"No");
if (range.location != NSNotFound) {
    // your code
}

NSLog 输出:

找到:是

注意:我更改了目标以证明不区分大小写的搜索有效.

Note: I changed the target to demonstrate that case insensitive search works.

选项可以或"在一起,包括:

The options can be "or'ed" together and include:

  • NSCaseInsensitiveSearch
  • NSLiteralSearch
  • NSBackwardsSearch
  • NSAnchoredSearch
  • NSNumericSearch
  • NSDiacriticInsensitiveSearch
  • NSWidthInsensitiveSearch
  • NSForcedOrderingSearch
  • NSRegularExpressionSearch

这篇关于搜索 NSString 是否包含值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:05