NSString字符串处理:截取字符串、匹配字符串、分隔字符串
1.截取字符串
NSString*string =@"wefsfsfsAdfsdf";
string =[string substringToIndex:5];//截取掉下标5之后的字符串
NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取掉下标2之前的字符串
NSLog(@"截取值为:%@",string);
2.匹配字符串
NSString*string =@"wefsfsfsAdfsdf";
NSRangerange =[stringrangeOfString:@"f"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange:NSMakeRange(3,4));
string =[string substringWithRange::NSMakeRange(3,4)];//截取范围类的字符串
NSLog(@"截取值为:%@",string);
3.分隔字符串
NSString*string =@"wefsfsfsAdfsdf";
NSArray *arr =[string componentsSeparatedByString:@"-"];//从字符-中分隔成2个元素的数组
NSLog(@"arr:%@",arr);//结果是adfsfsfs和dfsdf
4.数组转字符串
NSString *string =[array componentsJoinedByString:@","];,为分隔符