Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break NSString using an NSString, get everything after the string that was used to break/separate

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!

like image 855
Cole Avatar asked Nov 30 '22 18:11

Cole


2 Answers

Shorter:

[[getTheNameOuttaHere componentsSeparatedByString:@"1ZA"] lastObject];
like image 67
Paul Lynch Avatar answered Dec 05 '22 04:12

Paul Lynch


I would try using componentsSeparatedByString:

NSArray* components = [getTheNameOuttaHere componentsSeparatedByString:separatorString];

NSString* namePart = [components lastObject];

NSLog(@"name = %@", namePart);
like image 38
progrmr Avatar answered Dec 05 '22 03:12

progrmr