问题
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