How to split items in a string separated by ","
In my App, data comes in Str开发者_JAVA百科ing like this
"Hi,Hello,Bye"
I want to separate data by ","
How can I do that?
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];
NSString *str = @"Hi,Hello,Bye";
NSArray *aArray = [str componentsSeparatedByString:@","];
For more info, look at this post.
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
Use [myString componentsSeparatedByString:@","]
.
If it's NSString, you can use componentsSeparatedByString.
If it's std::string, you can iterate looking for the item (using find_frst_of and substr)
NSArray *components = [@"Hi,Hello,Bye" componentsSeparatedByString:@","];
Apple's String Programming Guide will help you get up to speed.
精彩评论