如何将带逗号分隔项的字符串转换为数组

时间:2012-04-20 09:21:47

标签: iphone objective-c ios arrays

  

可能重复:
  how can I convert string to an array with separator?

假设:

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

我想在这样的数组中用逗号分隔这些单词:

  

元素0 = Abc,

     

元素1 = Def,

     

元素2 = Ghi,

     

元素3 = Lmno。

5 个答案:

答案 0 :(得分:9)

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

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

答案 1 :(得分:4)

尝试使用此代码:

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

希望这有帮助。

答案 2 :(得分:2)

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

Nslog (@"%@",array);

答案 3 :(得分:0)

我想使用componentsSeparatedByString。 在apple documentation

中很容易找到这些方法

答案 4 :(得分:0)

`NSString *str = @"This, is, test, string";
NSArray *myArray= [[NSArray alloc]initWithObjects:[NSString stringWithFormat:@"%@",str ], nil];
NSLog(@"%@", myArray);`
相关问题