Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串ios中匹配字符的索引和范围_Ios_Iphone_Nsstring - Fatal编程技术网

字符串ios中匹配字符的索引和范围

字符串ios中匹配字符的索引和范围,ios,iphone,nsstring,Ios,Iphone,Nsstring,NSString中是否有内置函数执行以下操作 我有一个NSString名为str1,文本为@“牙科就诊”和str2文本为@“vis” 我想在str1中获取匹配字符str2的起始索引,并且仅当str2完全匹配时,才获取匹配范围 对于以上内容,我应该将索引设置为7,范围设置为3。我认为您正在寻找NSString的方法rangeOfString: NSRange range = [str1 rangeOfString:str2]; NSUInteger location = range.locatio

NSString
中是否有内置函数执行以下操作

我有一个
NSString
名为
str1
,文本为
@“牙科就诊”
str2
文本为
@“vis”

我想在
str1
中获取匹配字符
str2
的起始索引,并且仅当
str2
完全匹配时,才获取匹配范围


对于以上内容,我应该将索引设置为7,范围设置为3。

我认为您正在寻找
NSString
的方法
rangeOfString

NSRange range = [str1 rangeOfString:str2];
NSUInteger location = range.location;   // gives 7 per example
NSUInteger length = range.length;       // gives 3

如果在
str1
中找不到
str2
(或者
str2
为空:
@
),则返回
{NSNotFound,0}
。它将引发无效参数异常is
str1
is
nil
。否则,如果在
str1
中找到
str2
,它将返回一个
NSRange
结构,其中包含
str2
位于
str1

中的范围。在提出类似问题之前,请扫描参考文档。如果快速扫描文档中的
NSString
,就会回答这个问题。
[str1 rangeOfString:str2];