I have a lot of strings looking like this:
Soccer Livescore: (POR-SF) #Rio Ave vs #SC Braga: 2-1 Soccer Livescore: (ENG-FC) #Chester FC vs #Halifax: 1-0
How can i extract the string between the () and the ints on each side of the -?
I have a lot of strings looking like this:
Soccer Livescore: (POR-SF) #Rio Ave vs #SC Braga: 2-1 Soccer Livescore: (ENG-FC) #Chester FC vs #Halifax: 1-0
How can i extract the string between the () and the ints on each side of the -?
Probably you wanna look at NSString tokenization: NSString tokenize in Objective-C There is sample in one of the answers there:
NSString *string = @"oop:ack:bork:greeble:ponies";
NSArray *chunks = [string componentsSeparatedByString: @":"];
Refer to the following methods of NSString
:
rangeOfString:
(use it to look for '(' and ')').
substringWithRange:
(to get substring 'TM1-TM2').
componentsSeparatedByString:
(to split string you get by '-' character).
Here's the answer.
NSString *string =@"Soccer Livescore: (CONMEBOL-GS) #Rio Ave vs #SC Braga: 2-1";
NSRange range1 = [string rangeOfString:@"("];
NSRange range2 = [string rangeOfString:@")"];
range1.location = range1.location + 1;
range1.length = range2.location - range1.location;
NSString *scoreString = [string substringWithRange:NSMakeRange([string length]-3, 3)];
NSArray *scores = [scoreString componentsSeparatedByString:@"-"];
NSLog(@"%@ %d %d", [string substringWithRange:range1], [scores[0] intValue], [scores[1] intValue]);
Log result: CONMEBOL-GS 2 1