开发者

Extract 2 strings from an NSString separated by a special character

i have an NSString like "Hello - this is me". I want to search for the "-" and put the text before a开发者_开发问答nd after the "-" in two separate strings.

Anyone knows how to do that the best way?

greets Max


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.


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]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜