你正在尋找的方法是內置到NSString類中。該課程的文檔可以在here找到。
使用的方法是。
- (NSArray *)componentsSeparatedByString:(NSString *)separator
//An example of this in action
NSString *exampleString = @"Test|some|strings";
NSArray *separatedStrings = [exampleString componentsSeparatedByString:@"|"];
在這一點上,你可以做任何你將與陣列中的每個字符串喜歡諸如循環訪問,或訪問序列中的特定字符串。
我已經包含了迭代返回供參考的元素的示例。
//This for example is a nice use of the NSArray enumeration
//Much easier than the standard for loop with multiple parameters
for(NSString *aComponent in separatedStrings) {
NSLog(@"A piece is: %@", aComponent);
//Do something with each string
}
像這樣:http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c? –
可以統計字符串中的'|'的數量嗎? – Devang