我需要找一个字或几个字.然而,通过这种方法,我发现了一条字.
NSString *searchString = [NSString stringWithFormat:@"%@",searchField.text]; NSRange range = [textString rangeOfString : searchString]; if (range.location != NSNotFound) { NSLog(@"textString = %@",textString); }
我需要单词/单词
我能怎么做?
谢谢!
解决方法
在NSString中有各种解析/查找子字符串的方法:
> NSString本身
> NSRegularExpression.这可能更适合您的需求,因为您可以解决围绕单词周围的白色空间的情况.因此在寻找猫时不会从猫弹簧中归还猫.
> NSScanner(最有可能满足您的需求)
……当然,他们每个人都有自己的PRO和CON.
NSString有9种方法分组在“查找字符和子字符串”下.方法如:
-rangeOfString:
Finds and returns the range of the first occurrence of a given string within the receiver.
NSRegularExpression有5个方法分组在“使用正则表达式搜索字符串”下.方法如:
-numberOfMatchesInString: options: range:
Returns the number of matches of the regular expression within the specified range of the string.
了解NSScanner可能也很有用,但是如果你解析字符串而不是简单地查找子部分,这个类会更有用.