【发布时间】:2011-10-18 19:47:56
【问题描述】:
在我的应用程序中,数据以这样的字符串形式出现
"Hi,Hello,Bye"
我想用“,”分隔数据
我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4
在我的应用程序中,数据以这样的字符串形式出现
"Hi,Hello,Bye"
我想用“,”分隔数据
我该怎么做?
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4
使用[myString componentsSeparatedByString:@","]。
【讨论】:
NSString *str = @"Hi,Hello,Bye";
NSArray *aArray = [str componentsSeparatedByString:@","];
欲了解更多信息,请查看此post。
【讨论】:
如果是NSString,可以使用componentsSeparatedByString。
如果是 std::string,您可以迭代查找该项目(使用 find_frst_of 和 substr)
【讨论】:
NSArray *components = [@"Hi,Hello,Bye" componentsSeparatedByString:@","];
Apple's String Programming Guide 将帮助您快速上手。
【讨论】:
好吧,天真的方法是使用 componentsSeparatedByString:,正如其他答案中所建议的那样。
但是,如果您的数据确实是 CSV 格式,那么您最好考虑使用适当的 CSV 解析器,例如这个(我写的):https://github.com/davedelong/CHCSVParser
【讨论】:
使用 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];
【讨论】: