2011-08-03 55 views

回答

22

使用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]; 
+0

這是不正確的。正確的語法在下面的答案中註明:[str componentsSeparatedByString:@「,」]; –

2

使用[myString componentsSeparatedByString:@","]

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

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

欲瞭解更多信息,請看post

4

那麼,天真的做法將使用componentsSeparatedByString:,正如其他答案中所建議的。

但是,如果你的數據是真正的CSV格式,你會做得很好,可以考慮使用適當的CSV解析器,像這樣的(這是我寫的):https://github.com/davedelong/CHCSVParser

+0

CSV示例在未來會很有用。謝謝 – Devang