Elton's Blog

Objective-C中切分数组

by on 九.11, 2009, under Mac

在很多脚本语言如ruby,python中都有将字符串切分成数组或者将数组元素以某个间隔字符串间隔形成新的数组。 其实NSArray也提供了这样的功能。

使用-componentsSeparatedByString:来切分NSArray。 如:

1
2
NSString *string = @"white:black:blue:red";
NSArray *aArray = [string componentsSeparatedByString:@":"];

用-componentsJoinedByString:来合并NSArray中的各个元素并创建一个新的字符串,如:

1
string = [aArray componentsJoinedByString:@","];

这样,上面的数组就中的各个元素就以”,”分割形成一个字符串。





:,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...