Break NSString using NSString, get everything after the line that was used to break / split

I am trying to get DOE, JOHN from below NSString:

IDCHK9898960101DL00300171DL1ZADOE,JOHN

I tried to split the string into 1ZA since it will be constant.

Here is what I have tried so far, but this gives 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); 

Exit:

 container: IDCHK9898960101DL00300171DL 

Any help would be great! Thanks!

+4
source share
3 answers

In short:

 [[getTheNameOuttaHere componentsSeparatedByString:@"1ZA"] lastObject]; 
+15
source

I would try using componentsSeparatedByString :

 NSArray* components = [getTheNameOuttaHere componentsSeparatedByString:separatorString]; NSString* namePart = [components lastObject]; NSLog(@"name = %@", namePart); 
+3
source

componentsSeparatedByString works fine, 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); } 

Exit:

 2010-04-26 16:50:58.496 [25694:a0f] element: IDCHK9898960101DL00300171DL 2010-04-26 16:50:58.497 [25694:a0f] element: DOE,JOHN 
+2
source

Source: https://habr.com/ru/post/1308076/


All Articles