AERP|01,KSE;04,NCEL;05,GSE;|<END>
这是我的字符串现在我想将它分成两个数组,如
array1 = 01,04,05。 array2 = KSE,NCEL,GSE
并忽略"AERP|" & "|<END>".
注意:我在目标c。
工作答案 0 :(得分:3)
使用NSString
componentsSeparatedByCharactersInSet
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
构建您自己的NSCharacterSet
使用NSMutableCharacterSet
NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString@"|"];
答案 1 :(得分:3)
假设|
,;
,,
是重要性递减的分隔符,我首先删除开头和结尾,然后分成数字/助记符对,然后分开对。它会是这样的:
NSString* input = @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
NSArray* splits = [input componentsSeparatedByString: @"|"];
// In real life, check that splits has three elements
NSString* body = [splits objectAtIndex: 1];
splits = [body componentsSeparatedByString: @";"];
NSMutableArray* numbers = [[NSMutableArray alloc] init];
NSMutableArray* mnemonics = [[NSMutableArray alloc] init];
for (NSString* item in splits)
{
NSArray* parts = [item componentsSeparatedByString: @","];
if ([parts count] == 2)
{
[numbers addObject: [parts objectAtIndex: 0]]; // Could convert the part to a number here
// with NSDecimalNumber numberWithString: or alternative
[mnemonics addObject: [parts objectAtIndex: 1]];
}
}
答案 2 :(得分:1)
NSString *stringToParse = [string substringWithRange:NSMakeRange(5, [string length] - 7)];
/* stringToParse = @"01,KSE;04,NCEL;05,GSE" */
NSArray *keyValuePairs = [stringToParse componentsSeparatedByString:@";"];
NSMutableArray *indices = [NSMutableArray array];
NSMutableArray *labels = [NSMutableArray array];
for (NSString *keyValueString in keyValuePairs) {
NSArray *keyValuePairing = [keyValueString componentsSeparatedByString:@","];
[indices addObject:[keyValuePairing objectAtIndex:0]];
[labels addObject:[keyValuePairing objectAtIndex:1]];
}
答案 3 :(得分:1)
NSString *astring= @"AERP|01,KSE;04,NCEL;05,GSE;|<END>";
NSArray *aq=[astring componentsSeparatedByString:@"|"];
NSString *sw=[aq objectAtIndex:1];
NSArray *aq2=[sw componentsSeparatedByString:@","];
NSArray *aq3=[[aq2 objectAtIndex:1] componentsSeparatedByString:@";"];
NSArray *aq4=[[aq2 objectAtIndex:2] componentsSeparatedByString:@";"];
NSMutableArray *a1,*a2;
a1=[NSMutableArray array];
a2=[NSMutableArray array];
[a1 addObject:[aq2 objectAtIndex:0]];
[a1 addObject:[aq3 objectAtIndex:1]];
[a1 addObject:[aq4 objectAtIndex:1]];
[a2 addObject:[aq3 objectAtIndex:0]];
[a2 addObject:[aq4 objectAtIndex:0]];
[a2 addObject:[aq2 objectAtIndex:3]];
修改数组的名称,因为我已经快点完成了。