Split one string into different strings

后端 未结 5 590
不知归路
不知归路 2020-12-15 21:06

i have the text in a string as shown below

011597464952,01521545545,454545474,454545444|Hello this is were the message is.

Basically i woul

5条回答
  •  醉酒成梦
    2020-12-15 22:10

    I would use -[NSString componentsSeparatedByString]:

    NSString *str = @"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
    
    NSArray *firstSplit = [str componentsSeparatedByString:@"|"];
    NSAssert(firstSplit.count == 2, @"Oops! Parsed string had more than one |, no message or no numbers.");
    NSString *msg = [firstSplit lastObject];
    NSArray *numbers = [[firstSplit objectAtIndex:0] componentsSepratedByString:@","];
    
    // print out the numbers (as strings)
    for(NSString *currentNumberString in numbers) {
      NSLog(@"Number: %@", currentNumberString);
    }
    

提交回复
热议问题