Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split items in a string separated by ","

In my App, data comes in String like this

"Hi,Hello,Bye"

I want to separate data by ","

How can I do that?

like image 623
Ankit Chauhan Avatar asked Aug 03 '11 05:08

Ankit Chauhan


3 Answers

use componentsSeparatedByString:

NSString *str = @"Hi,Hello,Bye";  
NSArray *arr = [str componentsSeparatedByString:@","];  
NSString *strHi = [arr objectAtIndex:0];  
NSString *strHello = [arr objectAtIndex:1];
NSString *strBye = [arr objectAtIndex:2];
like image 145
Nitish Avatar answered Sep 20 '22 16:09

Nitish


NSString *str = @"Hi,Hello,Bye";

NSArray *aArray = [str componentsSeparatedByString:@","];

For more info, look at this post.

like image 23
Ilanchezhian Avatar answered Sep 17 '22 16:09

Ilanchezhian


Well, the naïve approach would be to use componentsSeparatedByString:, as suggested in the other answers.

However, if your data is truly in the CSV format, you'd do well to consider using a proper CSV parser, such as this one (which I wrote): https://github.com/davedelong/CHCSVParser

like image 37
Dave DeLong Avatar answered Sep 20 '22 16:09

Dave DeLong