NSString 在整个文本中搜索另一个字符串

NSString search whole text for another string(NSString 在整个文本中搜索另一个字符串)
本文介绍了NSString 在整个文本中搜索另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个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 在整个文本中搜索另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
HTTPS request using volley(使用 volley 的 HTTPS 请求)