iOS字串分割常用方法
1.字串的替換:
NSString *str=@"12334dllggg33dlrt ";
str=[str stringByReplacingOccurrencesOfString:@"33"withString:@"hh"];
NSLog(@"%@",str);
輸出結果:12hh4dlggghhdlrt
2.通過range分割字串NSString *str=@"0123456&my_type=\"dlrthh ";
NSRange range = [str rangeOfString:@"&my_type=\""];
NSString *subStr = [str
NSLog(@"%@",subStr);
輸出結果:0123456 3.在某個range中查詢字串- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 熟悉該方法的使用
// NSCaseInsensitiveSearch = 1,//不區分大小寫
// NSLiteralSearch = 2,//區分大小寫
// NSBackwardsSearch = 4,//從字串末尾開始搜尋
// NSAnchoredSearch = 8,//搜尋限制範圍的字串
// NSNumbericSearch = 64//按照字串裡的數字為依據,算出順序。例如 Foo2.txt < Foo7.txt < Foo25.txt
NSString *str=@"0123456&my_type=\"dlrthhkkll\" ";
NSRange range = [str rangeOfString:@"&my_type=\""];
//&my_type=\"之後的range
range.location += range.length
range.length = [str length] - range.location;
NSRange range2 = [str rangeOfString:@"\""options:NSCaseInsensitiveSearchrange:range];
range.length = range2.location - range.location;
NSString *subStr = [str substringWithRange:range];
NSLog(@"%@",subStr);
輸出結果:dlrthhkkll
4.根據子字串分割字串
NSString *str2=@"0123456=my_type=\"dlrthhkkll\" ";
NSArray *temp=[str2 componentsSeparatedByString:@"="];
NSLog(@"%@",[temp description]);
輸出結果:(
0123456,
"my_type",
"\"dlrthhkkll\" "
)