2012-04-20 65 views
-1

可能重複:
how can I convert string to an array with separator?如何轉換一個字符串逗號分隔的項目到數組

假設:

string = @"Abc, Def, Ghi, Lmno"; 

我想這些話,這是用英文逗號分隔,如下所示:

元素0 = ABC,

元件1 =默認值,

元件2 = GHI,

元件3 = LMNO。

+0

發現U可以搜索這個鏈接更多的幫助http://stackoverflow.com/questions/6111543/how-can-i-convert-string-to-an- array-with-separator – Kiran 2012-04-20 09:25:34

+2

在詢問多次提問之前,請使用堆棧溢出搜索功能。 – 2012-04-20 09:37:13

回答

9
NSArray *myArray = [myString componentsSeparatedByString:@","]; 

[myArray objectAtIndex:0];//Abc 
[myArray objectAtIndex:1];//Def 
[myArray objectAtIndex:2];//Ghi 
[myArray objectAtIndex:3];//Lmno 
4

試試這個代碼了:

NSArray *words = [string componentsSeparatedByString:@","];

希望這有助於。

2
yourstring = @"Abc, Def, Ghi, Lmno"; 
NSArray *array = [yourstring componentsSeparatedByString:@","]; 

Nslog (@"%@",array); 
0
`NSString *str = @"This, is, test, string"; 
NSArray *myArray= [[NSArray alloc]initWithObjects:[NSString stringWithFormat:@"%@",str ], nil]; 
NSLog(@"%@", myArray);` 
相關問題