问题描述
我想在另一个NSString中搜索一个NSString,这样即使第二个不以第一个开头也能找到结果,例如:
I would like to search for an NSString in another NSString, such that the result is found even if the second one does not start with the first one, for example:
例如:我有一个搜索字符串st".我查看下面的记录,看看下面是否有任何包含这个搜索字符串,它们都应该返回一个好的结果,因为它们都有st".
eg: I have a search string "st". I look in the following records to see if any of the below contains this search string, all of them should return a good result, because all of them have "st".
餐厅
稳定
克尔斯滕
目前我正在做以下事情:
At the moment I am doing the following:
NSComparisonResult result = [selectedString compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
这仅适用于上例中的stable",因为它以st"开头,而其他 2 则失败.如何修改此搜索以使其对所有 3 都返回 ok?
This works only for "stable" in the above example, because it starts with "st" and fails for the other 2. How can I modify this search so that it returns ok for all the 3?
谢谢!!!
推荐答案
为什么不先google?
Why not google first?
字符串包含objective-c中的字符串
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}
这篇关于NSString 在整个文本中搜索另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!