PDA

View Full Version : Array created from separator?




Buschmaster
Sep 3, 2008, 09:37 PM
Anyone know of an easy way to create an array from a designated separator in obj-c?

What I mean is I want to create an array like
{hello,world,how,are,you}

out of something like:
@"hello:world:how:are:you"

There is a way around this for me but if someone knows of a way to do this I can really cut out quite a few middle men.



lee1210
Sep 3, 2008, 09:46 PM
Anyone know of an easy way to create an array from a designated separator in obj-c?

What I mean is I want to create an array like
{hello,world,how,are,you}

out of something like:
@"hello:world:how:are:you"

- (NSArray *)componentsSeparatedByString:(NSString *)separator

Something like:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *myString = @"Hello:World,:how:are:you?";
NSArray *myComponents = [myString componentsSeparatedByString:@":"];
int compLength = [myComponents count];
unsigned int x = 0;
for(x = 0; x < compLength; x++) {
NSLog(@"Component %u is: %@",x+1,[myComponents objectAtIndex:x]);
}
[pool release];
return 0;
}

I am assuming you wanted an NSArray of NSStrings, but you didn't say for sure. You can get a real id array from this, but I wouldn't recommend it.

-Lee

Eh, just in case:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *myString = @"Hello:World,:how:are:you?";
NSArray *myComponents = [myString componentsSeparatedByString:@":"];
int compLength = [myComponents count];
id *myTerribleCStyleArray = malloc(compLength * sizeof(id));
[myComponents getObjects:myTerribleCStyleArray];

unsigned int x = 0;
for(x = 0; x < compLength; x++) {
NSString *oneString = myTerribleCStyleArray[x];
NSLog(@"Component %u is: %@",x+1,oneString);
}

[pool release];
return 0;
}


This will be equivalent to:

NSString *mySecondTerribleArray[] = {@"Hello",@"World,",@"how",@"are",@"you?"};
for(x = 0; x < 5; x++){
NSString *oneString = mySecondTerribleArray[x];
NSLog(@"Component %u is: %@",x+1,oneString);
}


This most resembles what you posted, but again, I hope it's not what you really want.

-Lee

Buschmaster
Sep 3, 2008, 09:52 PM
- (NSArray *)componentsSeparatedByString:(NSString *)separator

Something like:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *myString = @"Hello:World,:how:are:you?";
NSArray *myComponents = [myString componentsSeparatedByString:@":"];
int compLength = [myComponents count];
unsigned int x = 0;
for(x = 0; x < compLength; x++) {
NSLog(@"Component %d is: %@",x+1,[myComponents objectAtIndex:x]);
}
[pool release];
return 0;
}

I am assuming you wanted an NSArray of NSStrings, but you didn't say for sure. You can get a real id array from this, but I wouldn't recommend it.

-Lee
Ah, componenetsSeparatedByString is just what I was looking for! Thanks!

lee1210
Sep 3, 2008, 09:59 PM
Got edit happy.

Anyway, the reference documents are your dearest friends:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

-Lee