我有一个像“你好 - 这就是我”的NSString。 我想搜索“ - ”并将“ - ”前后的文本放在两个单独的字符串中。
任何人都知道如何以最好的方式做到这一点?
迎接Max
答案 0 :(得分:45)
NSArray *subStrings = [myString componentsSeparatedByString:@"-"]; //or rather @" - "
NSString *firstString = [subStrings objectAtIndex:0];
NSString *lastString = [subStrings objectAtIndex:1];
//Add some array range checking to it and you're done.
答案 1 :(得分:6)
NSString *myString = @"123-456-789-1234-2345-3456-4567";
NSArray *subStrings = [myString componentsSeparatedByString:@"-"];
for (int i = 0; i < [subStrings count]; i++) {
NSLog(@"string on array position %d is : %@", i, [subStrings objectAtIndex:i]);
}