在开发中,我们经常要对字符串NSString进行处理.下面记录一下NSString的相关处理,以便日后查阅.
NSString的截取
NSString*string =@”sdfsfsfsAdfsdf”;
string = [string substringToIndex:7];//截取下标7之后的字符串
NSLog(@”截取的值为:%@”,string);
[string substringFromIndex:2];//截取下标2之前的字符串
NSLog(@”截取的值为:%@”,string);
NSString的匹配
NSString*string =@”sdfsfsfsAdfsdf”;
NSRangerange = [stringrangeOfString:@”f”];//匹配得到的下标
NSLog(@”rang:%@”,NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串
NSLog(@”截取的值为:%@”,string);
NSString的分隔
NSString*string =@“sdfsfsfsAdfsdf”;
NSArray *array = [string componentsSeparatedByString:@”A”]; //从字符A中分隔成2个元素的数组
NSLog(@”array:%@”,array); //结果是adfsfsfs和dfsdf