问题
In my App, data comes in String like this
"Hi,Hello,Bye"
I want to separate data by ","
How can I do that?
回答1:
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];
回答2:
NSString *str = @"Hi,Hello,Bye";
NSArray *aArray = [str componentsSeparatedByString:@","];
For more info, look at this post.
回答3:
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
回答4:
Use [myString componentsSeparatedByString:@","]
.
回答5:
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)
回答6:
NSArray *components = [@"Hi,Hello,Bye" componentsSeparatedByString:@","];
Apple's String Programming Guide will help you get up to speed.
来源:https://stackoverflow.com/questions/6922131/how-to-split-items-in-a-string-separated-by