Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa NSString explode

I have a NSString: @"1a,1b,1c,1d,5c". I want this NSString separated into a NSMutableArray, but I don't know how. I think it is fairly simple but I can't find it (maybe because my English isn't good enough to find a good description for it to search on).

Regards, Dodo

like image 346
dododedodonl Avatar asked May 01 '10 10:05

dododedodonl


3 Answers

NSString *_stringToSplit = @"1a,1b,1c,1d,5c";
NSArray *_splitItems = [_stringToSplit componentsSeparatedByString:@","];
NSMutableArray *_mutableSplitItems = [NSMutableArray arrayWithCapacity:[_splitItems count]]; 
[_mutableSplitItems addObjectsFromArray:_splitItems];
like image 166
Alex Reynolds Avatar answered Nov 03 '22 10:11

Alex Reynolds


[NSMutableArray arrayWithArray:[string componentsSeparatedByString:@","]];
like image 42
Paul Lynch Avatar answered Nov 03 '22 10:11

Paul Lynch


Use -componentsSeparatedByString: to explode.

The returned value is an NSArray. If you need an NSMutableArray, call the -mutableCopy method on it.

like image 3
kennytm Avatar answered Nov 03 '22 09:11

kennytm