在同一个分隔符上多次拆分 NSString [英] Split NSString multiple times on the same separator
本文介绍了在同一个分隔符上多次拆分 NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前收到这样的字符串:
I am currently receiving a string like this:
@"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]];
}
我现在会收到这样的字符串:
I will now be receiving a string like this:
@"Sam|26|Developer,Hannah|22|Team Leader,Adam|30|Director,Carlie|32|PA,Jan|54|Cleaner"
我可以(以及如何)使用与上述相同的方法多次使用|"分隔字符串吗?分隔符?
Can I (and how) use the same method as above to separate the string more than once using the "|" separator?
推荐答案
下面这行...
testArray2 = [s componentsSeparatedByString:@"|"];
将导致数组现在包含 3 个项目,而不是 2 个......无需再次拆分!
will cause the array to now contain 3 items, instead of 2..... no need to split again!
这篇关于在同一个分隔符上多次拆分 NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文