NSString 字符串查找与截取

1,字符串查找

NSString *str = @"menglingling";
NSRange range = [str1 rangeOfString:@"engl"];
NSLog(@"location = %lu, length = %lu",range1.location,range1.length);

2,字符串截取

(1)去掉与

//原字符串带有头和尾
NSString *str = @"i love u";

//确定range的location 与length
1,通过“>”的location定位“i”的location,
2,通过“>”与“"].location +1;
NSUInteger length = [str rangeOfString:@"

(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);

注:字符串在查找和截取过程中,遇到第一个符合条件的内容,就会返回,不再继续进行操作了

你可能感兴趣的:(NSString 字符串查找与截取)