2014-01-15 25 views
0

我有一個字符串,需要將其分成多個字符串。 例如,這是我的字符串:如何用兩個特殊字符分隔NSString?

#titleone%&蘋果*#titletwo%&蜂蜜*每次讀取「#」我需要它來保存字符串數組,當它讀取停止時間

「%」。此外,每次它讀取「&」我需要將字符串保存在另一個數組中,並在讀取「*」時停止。 到目前爲止IV做到了這一點

NSString* foo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSArray* stringComponents = [foo componentsSeparatedByString:@"#"]; 
NSArray* otherstringComponents = [foo componentsSeparatedByString:@"%"]; 

我只需要知道如何每次讀取某個字符時添加到一個數組,並在每次讀取另一個字符時停止。 或者如果有另一種方式來做這項工作,那麼我會明白這一點。

回答

0

您最好使用NSScanner並掃描到您的一組起始字符(使用scanUpToCharactersFromSet:intoString:),然後檢查決定如何處理從掃描到結束字符所獲得的字符串。