将一个字符串分割成不同的字符串

[英]Split one string into different strings


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

我想把它从一个包含它的字符串中分离出来

5 个解决方案

#1


45  

I would use -[NSString componentsSeparatedByString]:

我会使用-[NSString componentsseparation bystring]:

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

#2


5  

Look at NSString componentsSeparatedByString or one of the similar APIs.

看看NSString componentsseparation bystring或类似的api之一。

If this is a known fixed set of results, you can then take the resulting array and use it something like:

如果这是已知的固定结果集,那么您可以取结果数组,并使用如下内容:

NSString *number1 = [array objectAtIndex:0];    
NSString *number2 = [array objectAtIndex:1];
...

If it is variable, look at the NSArray APIs and the objectEnumerator option.

如果是变量,请查看NSArray api和objectEnumerator选项。

#3


1  

NSMutableArray *strings = [[@"011597464952,01521545545,454545474,454545444|Hello this is were the message is." componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",|"]] mutableCopy];

NString *message = [[strings lastObject] copy];
[strings removeLastObject];

// strings now contains just the number strings
// do what you need to do strings and message

....

[strings release];
[message release];

#4


0  

does objective-c have strtok()?

objective - c有strtok()?

The strtok function splits a string into substrings based on a set of delimiters. Each subsequent call gives the next substring.

strtok函数基于一组分隔符将字符串分割成子字符串。每个后续调用都给出下一个子字符串。

substr = strtok(original, ",|");
while (substr!=NULL)
{
   output[i++]=substr;
   substr=strtok(NULL, ",|")
}

#5


0  

Here's a handy function I use:

这是我使用的一个方便的功能:

///Return an ARRAY containing the exploded chunk of strings
///@author: khayrattee
///@uri: http://7php.com
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
    return [stringToBeExploded componentsSeparatedByString: delimiter];
}
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/08/12/4412990319f475ac881318d7a7fee377.html



猜您在找
如何在C中将一个字符串分割成两个字符串 - How to split a string to 2 strings in C 如何将一个字符串分割成句子可可 - How to split a string into sentences cocoa 将字符串分割成一个字符 - Splitting A String Up To A Character 如何将一个字符串分割成给定长度的子串?(复制) - How to split a string into substrings of a given length? [duplicate] 怎么样把一个字符串分割成单个字符数组?
智能推荐
 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告