如何将NSString转换为数组?

3
例如,我读取了这样的数据:
a\tbcd\tttte\tjjjd\tnjnjnjd\tss\tee

我想创建一个像这样的数组:

{ @"a", @"bcd", @"ttte", @"jjjd", @"njnjnjd", @"ss", @"ee" }

我该怎么做呢?谢谢。
2个回答

31
你可以使用-componentsSeparatedByString:,例如:
NSArray *ary = [mystring componentsSeparatedByString:@"\t"];

2
- (NSArray *)componentsSeparatedByString:(NSString *)separator 

componentsSeparatedByString: 方法:返回一个数组,该数组包含由给定分隔符分割的接收器中的子字符串。
参数 separator:分隔符字符串。
返回值:一个包含来自接收器的已由分隔符分割的子字符串的NSArray对象。
讨论:数组中的子字符串按它们在接收器中出现的顺序出现。分隔符字符串的相邻出现在结果中产生空字符串。同样,如果字符串以分隔符开头或结尾,则第一个或最后一个子字符串为空。例如,以下代码片段:
 NSString *list = @"Norman, Stanley, Fletcher";
 NSArray *listItems = [list componentsSeparatedByString:@", "];
produces an array { @"Norman",@"Stanley", @"Fletcher" }.

If list begins with a comma and space—for example, ", Norman, Stanley, Fletcher"—the array has these contents: { @"", @"Norman", @"Stanley", @"Fletcher" }

If list has no separators—for example, "Norman"—the array contains the string itself, in this case { @"Norman" }.

Availability Available in Mac OS X v10.0 and later.

See Also:

componentsJoinedByString: (NSArray)

– pathComponents

Related Sample Code:

ColorMatching

CoreRecipes

iSpend

iSpendPlugin

QTKitMovieShuffler

Declared In NSString.h

来自NSString文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接