开发过程中真是会用到各种各样的方法,今天记录一下字符串和数组的相互转换的方法
###1.将string字符串转换为array数组 (componentsSeparatedByString)
//需要转换的字符串
NSString *string = @"我,们,都,是,来,学,习,的";
// " , "为分割符号 可根据需要自行设置
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array === %@",array);
复制代码
打印信息
array === (
"\U6211",
"\U4eec",
"\U90fd",
"\U662f",
"\U6765",
"\U5b66",
"\U4e60",
"\U7684"
)
复制代码
###2.将array数组转换为string字符串 (componentsJoinedByString)
//需要成字符串的数组
NSArray *array = @[@"我", @"们", @"都", @"是", @"来", @"学", @"习", @"的"];
// " , "为分割符号 可根据需要自行设置
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string === %@",string);
复制代码
打印信息
string === 我,们,都,是,来,学,习,的
复制代码