我目前收到这样的字符串:
@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54"
我将其拆分为:
testArray = [[NSArray alloc] init];
NSString *testString = [[NSString alloc] initWithFormat:@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30"];
testArray = [testString componentsSeparatedByString:@","];
dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {
testArray2 = [s componentsSeparatedByString:@"|"];
[dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
}
我现在将收到这样的字符串:
@"Sam|26|Developer,Hannah|22|Team Leader,Adam|30|Director,Carlie|32|PA,Jan|54|Cleaner"
我可以(以及如何)使用与上述相同的方法使用“ |”多次分隔字符串。分隔器?
NSString
“分离”方法,并在此偶然发现答案。