How to split a string using a delimiter in iPhone and add it to a mutable array?

By | February 10, 2014

Here is a simple code that does this..

-(NSMutableArray *) getAnswers :(NSString *) yourstring{
    NSMutableArray *answers = [NSMutableArray new];
    NSArray *parts = [[yourstring componentsSeparatedByString:@","];
    for(NSString *str in parts){
        [answers addObject:str];
    }
    NSLog(@"RET answers %@", answers);
    return answers;
}

After splitting the string with “componentsSeparatedByString” the parts are added to a NSMutableArray using addObject.

Leave a Reply

Your email address will not be published. Required fields are marked *