How to split items in a string separated by “,”

爷,独闯天下 提交于 2019-12-30 04:58:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!