2 mapmapm MapMapM 于 2013.02.28 11:15 提问

将一个字符串分割成多个字符串

有一个字符串:

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

我希望每串数字都能在不同string中,

NSString *Number1 = 011597464952 
NSString *Number2 = 01521545545
etc
etc
NSString *Message = Hello this is were the message is.

怎么样完整的将字符串分割?

1个回答

prettYYoyic
prettYYoyic   2013.02.28 11:32
已采纳

-[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 number) {
  NSLog(@"Number: %@", currentNumberString);
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!