views:

169

answers:

3

I'm trying to get the DOE,JOHN from the below NSString:

IDCHK9898960101DL00300171DL1ZADOE,JOHN

I was trying to split the string on 1ZA, as that will be constant.

Here's what I've tried so far, but it's giving me the opposite of what I'm looking for:

 NSString *getTheNameOuttaHere = @"IDCHK9898960101DL00300171DL1ZADOE,JOHN";

 // scan for "1ZA"
 NSString *separatorString = @"1ZA";

 NSScanner *aScanner = [NSScanner scannerWithString:getTheNameOuttaHere];

 NSString *thingsScanned;

 [aScanner scanUpToString:separatorString intoString:&thingsScanned];

 NSLog(@"container: %@", thingsScanned);

Output:

container: IDCHK9898960101DL00300171DL

Any help would be great! Thanks!

+1  A: 

componentsSeparatedByString worked great, see below:

NSString *name = @"IDCHK9898960101DL00300171DL1ZADOE,JOHN";

// scan for "1ZA"
NSString *separatorString = @"1ZA";

NSArray *split = [name componentsSeparatedByString:separatorString];

for (NSString *element in split) {
    NSLog(@"element: %@", element);
}

Output:

2010-04-26 16:50:58.496 [25694:a0f] element: IDCHK9898960101DL00300171DL
2010-04-26 16:50:58.497 [25694:a0f] element: DOE,JOHN
Cole
If someone answers correctly, you probably should add a comment to their answer with any additional details or results then mark it correct, rather than re-writing their answer and creating a new one.
Nick Forge
uhh... i answered my own question before any additional answers were posted...check the timestamps (21:53 < 21:54)...i marked Paul's response... i like one liners.
Cole
+2  A: 

I would try using componentsSeparatedByString:

NSArray* components = [getTheNameOuttaHere componentsSeparatedByString:separatorString];

NSString* namePart = [components lastObject];

NSLog(@"name = %@", namePart);
progrmr
A: 

Shorter:

[[getTheNameOuttaHere componentsSeparatedByString:@"1ZA"] lastObject];
Paul Lynch
I appreciate your response! I had my blinders on when initially thinking about using componentsSeparatedByString - thinking that it could only take a string of length 1 (i.e. ",", ";", etc..) - which is not the case at all :D
Cole