获取子字符串的位置
- (NSRange)rangeOfString:(NSString *)aString;
根据位置获取子字符串
- (NSString *)substringWithRange:(NSRange)range;
示例:
// NSRange 用法 NSString *fatherStr = @"Apple, Banner, Orange"; NSString *subStr_1 = @"Banner"; NSRange range_1 = [fatherStr rangeOfString:subStr_1]; NSLog(@"%@", NSStringFromRange(range_1)); NSRange range_2 = NSMakeRange(0, 5); NSString *subStr_2 = [fatherStr substringWithRange:range_2]; NSLog(@"%@", subStr_2);
输出结果:
2015-03-02 17:34:35.479 XYResponseTimeWithC[11160:303] {7, 6} 2015-03-02 17:34:35.480 XYResponseTimeWithC[11160:303] Apple