美文网首页
查找子字符串在父串中的位置

查找子字符串在父串中的位置

作者: 写代码的八爪鱼 | 来源:发表于2017-11-18 01:28 被阅读22次

经常会遇到在一串字符串中查找匹配的子字符串位置,特记录下
NSString *string1 = @"This is hello world ";
NSString *string2 = @"is";
NSRange range = [string1 rangeOfString:string2];
NSLog(@"length:%d..location: %d.",range.length,range.location);

总结:
//父字符串中未包含子字符串
range.length == 0 && range.location为很大的随机数

//父字符串中包含子字符串
range.length 为子字符串长度; range.location为子串在父串的起始位置
如果父串中包含多个子串,查询出的range.location为父串中的第一个匹配的子串的位置

相关文章

网友评论

      本文标题:查找子字符串在父串中的位置

      本文链接:https://www.haomeiwen.com/subject/ssnsvxtx.html