一、NSString的截取
1.从字符串开头开始截取到指定位置(不包含该位置)
NSString * string = @"abcdef";
NSString * subString1 = [string substringToIndex:3];
NSLog(@"%@", subString1);
输出abc
2.从字符串指定位置开始截取到最后(包含该位置)
NSString * string = @"abcdef";
NSString * subString2 = [string substringFromIndex:3];
NSLog(@"%@", subString2);
输出def
NOTE:字符串第一位index值为0;
二、NSString的拼接
字符串拼接有三种方法
1.stringWithFormat
NSString * string1 = @"123";
NSString * string2 = @"456";
NSString * string3 = [NSString stringWithFormat:@"%@%@", string1, string2];
NSLog(@"%@", string3);
输出123456
2.stringByAppendingString
NSString * string4 = [string1 stringByAppendingString:string2];
NSLog(@"%@", string4);
输出123456
3.stringByAppendingFormat
NSString * string5 = [string1 stringByAppendingFormat:@"%@", string2];
NSLog(@"%@", string5);
输出123456
三、NSString的查询
NSString * string = @"abcdef";
NSString * str = @"abc";
1.查询是否以某字符串开头
BOOL isOrNot = [string hasPrefix:str];
2.查询是否以某字符串结尾
BOOL isOrNot = [string hasSuffix:str];
3.查询是否包含某字符串
NSRange range = [string rangeOfString:str];
NSLog(@"%ld, %ld", range.location, range.length); // location为查询字符串所在位置,length为查询字符串的长度
输出0, 3
四、NSString的分割
NSString * string = @"abc|def";
// 分割方法返回的是数组
NSArray * arr = [string componentsSeparatedByString:@"|"];
NSLog(@"%@", arr);
输出 (abc, def)