程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> string-將一個字符串分割成多個字符串

string-將一個字符串分割成多個字符串

編輯:編程綜合問答
將一個字符串分割成多個字符串

有一個字符串:

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.

怎麼樣完整的將字符串分割?

最佳回答:


-[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);
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved