NSArray 与字符串
1.把数组元素链接成字符串
- - (NSString )componentsJoinedByString:(NSString )separator;
- 这是NSArray的方法, 用separator作拼接符将数组元素拼接成一个字符串
NSArray *arr = @[@"lnj", @"lmj", @"jjj", @"xcq"]; NSString *res = [arr componentsJoinedByString:@"*"]; NSLog(@"res = %@", res); 输出结果: lnj*lmj*jjj*xcq
2.字符串分割方法
- - (NSArray )componentsSeparatedByString:(NSString )separator;
- 这是NSString的方法,将字符串用separator作为分隔符切割成数组元素
NSString *str = @"lnj-lmj-jjj-xcq"; NSArray *arr = [str componentsSeparatedByString:@"-"]; NSLog(@"%@", arr); 输出结果: ( lnj, lmj, jjj, xcq )