将一个字符串拆分为不同的字符 [英] Split one string into different strings

查看:95
本文介绍了将一个字符串拆分为不同的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串中的文字,如下所示

i have the text in a string as shown below

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

基本上我希望不同字符串中的每个数字都能显示消息,例如

Basically i would like each of the numbers in different strings to the message eg

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

我希望从一个包含所有内容的字符串中拆分

i would like to have that split out from one string that contains it all

推荐答案

我会使用 - [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);
}

这篇关于将一个字符串拆分为不同的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆