简体   繁体   中英

How to Split a string in iphone?

AERP|01,KSE;04,NCEL;05,GSE;|<END> 

this is my string now i want to split it in two arrays like

array1 = 01,04,05. array2 = KSE,NCEL,GSE

and ignore "AERP|" & "|<END>". "AERP|" & "|<END>".

Note: I am working in objective c.

Use the NSString componentsSeparatedByCharactersInSet

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

To construct your own NSCharacterSet use NSMutableCharacterSet

NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString@"|"];

Assuming that | , ; , , are separators of decreasing significance, I would first remove the beginning and the end, then separate into number/mnemonic pairs, then separate the pairs. It would go something like this:

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]];
    }
}
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]];
}
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]];

modify the names of the array as I have done it in hurry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM