1,字符串查找
NSString *str = @"menglingling";
NSRange range = [str1 rangeOfString:@"engl"];
NSLog(@"location = %lu, length = %lu",range1.location,range1.length);
2,字符串截取
(1)去掉<head>与</head>
//原字符串带有<head>头和</head>尾
NSString *str = @"<head>i love u</head>";
//确定range的location 与length
1,通过“>”的location定位“i”的location,
2,通过“>”与“</”的length差值,确定“i love u”正文长度
NSUInteger location = [str rangeOfString:@">"].location +1;
NSUInteger length = [str rangeOfString:@"</"].location - location;
//NSUInteger length = [str rangeOfString:@"<" options:NSBackwardsSearch].location - location;//需要注意str中有两个<
NSRange range = NSMakeRange(location, length);
//根据range,在原字符串中截取后得到新字符串
NSString *newStr = [str substringWithRange:range2];//返回一个新的字符串
NSLog(@"str = %@",str);
NSLog(@"newStr = %@",newStr);//原字符串没有改变,生成新字符串
(2)从url中截取最后部分
NSString *str = @"http://www.91chuansha.com/files/pw/pwclothdetail.html?1410387956107264";
NSUInteger location = [str rangeOfString:@"?" options:NSBackwardsSearch].location +1;//从后向前查找“?”,并截取?之后部分内容,不包括?
NSString *newStr = [str substringFromIndex:location];
NSLog(@"newStr = %@",newStr);
注:字符串在查找和截取过程中,遇到第一个符合条件的内容,就会返回,不再继续进行操作了