NSString *str = @"yuaixing"; //1.从指定位置开始截取到最后,包含指定位置 NSString *str2 = [str substringFromIndex:3];
//2.从开始位置截取到指定位置,不包含指定位置 NSString *str3 = [str substringToIndex:3];
//3.截取一个范围 range NSRange r = {3,4}; NSString *str4 = [str substringWithRange:r]; NSLog(@"%@",str4); //结果:ixin
//4.取出"传智播客"四个字 NSString *str = @"<itcast>传智播客</itcast>"; NSUInteger loc = [str rangeOfString:@">"].location + 1; NSUInteger len = [str rangeOfString:@"</"].location - loc; NSRange r = {loc,len}; NSString *str2 = [str substringWithRange:r]; NSLog(@"\n%@",str2);
//5.字符串的替换 NSString *str = @"yuaixing"; NSString *newStr = [str stringByReplacingOccurrencesOfString:@"ai" withString:@"ooo"];
NSLog(@"\n %@",newStr);
|