1 /**
2 * substringFromIndex:字符串截取方法,从某索引开始截取到最后
3 * substringToIndex:从索引0开始截取到某索引
4 * substringWithRange:从某索引开始,截取一定长度的字符
5 * substringByReplacingOccurrencesOfString:字符串替换
6 */
7 #import <Foundation/Foundation.h>
8
9 int main(int argc, const char * argv[]) {
10 @autoreleasepool {
11
12 //字符串截取
13 NSString *str1=@"Hello world";
14 //从索引为2的位置开始截取直到最后(包括第二个字符)
15 NSLog(@"%@",[str1 substringFromIndex:2]);//打印结果为:llo world
16 //从索引为0开始截取到索引为2的位置,但是不包括索引2所对应的字符
17 NSLog(@"%@",[str1 substringToIndex:2]);//打印结果为:He
18 //从指定索引的位置(location)向后截取,截取长度为length
19 NSRange range={2,3};//截取位置从索引2开始 截取3位长度的字符 包括索引为2对应的字符
20 NSLog(@"%@",[str1 substringWithRange:range]);//打印结果:llo
21
22 //字符串替换
23 NSString *str2=@"abc";
24 NSString *str3=@"def";
25 NSLog(@"%@",[str2 stringByReplacingOccurrencesOfString:str2 withString:str3]);//打印结果:def
26
27 }
28 return 0;
29 }