Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a substring from an NSString until arriving to a specific word

Let us say I have this NSString: @"Country Address Tel:number". How can I do to get the substring that is before Tel? (Country Address ) And then How can I do to get the substring that is after Tel? (number)

like image 952
Guy Daher Avatar asked Feb 11 '12 19:02

Guy Daher


1 Answers

Use NSScanner:

NSString *string = @"Country Address Tel:number";
NSString *match = @"tel:";
NSString *preTel;
NSString *postTel;

NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner scanUpToString:match intoString:&preTel];

[scanner scanString:match intoString:nil];
postTel = [string substringFromIndex:scanner.scanLocation];

NSLog(@"preTel: %@", preTel);
NSLog(@"postTel: %@", postTel);

NSLog output:

preTel: Country Address
postTel: number

like image 86
zaph Avatar answered Sep 20 '22 10:09

zaph