How can I convert the NSString to a array?

↘锁芯ラ 提交于 2019-12-05 02:24:20

问题


For example, I read a data like this

a\tbcd\tttte\tjjjd\tnjnjnjd\tss\tee

and I want to make a array like this:

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

How can I do so? Thank you.


回答1:


You can use -componentsSeparatedByString:, as in

NSArray *ary = [mystring componentsSeparatedByString:@"\t"];



回答2:


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

componentsSeparatedByString: Returns an array containing substrings from the receiver that have been divided by a given separator.

  • (NSArray *)componentsSeparatedByString:(NSString *)separator Parameters separator The separator string. Return Value An NSArray object containing substrings from the receiver that have been divided by separator.

Discussion The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:

 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

From NSString docs



来源:https://stackoverflow.com/questions/4814130/how-can-i-convert-the-nsstring-to-a-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!