0

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 -?

Phillip Kinkade
  • 1,382
  • 12
  • 18
user3258468
  • 354
  • 7
  • 18
  • Can you provide more information. You can also provide the actual output which you want... So it will more helpful for us. – V.J. Feb 14 '14 at 08:06
  • Please check [NSString Class Reference](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSString_Class/Reference/NSString.html) – Midhun MP Feb 14 '14 at 08:07
  • possible duplicate of [NSString tokenize in Objective-C](http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c) – Krypton Feb 14 '14 at 08:11

3 Answers3

0

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: @":"];
Community
  • 1
  • 1
Krypton
  • 3,337
  • 5
  • 32
  • 52
0

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).

kovpas
  • 9,553
  • 6
  • 40
  • 44
0

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

Steve Ham
  • 3,067
  • 1
  • 29
  • 36
  • This will get the first 6 characters after the (. Can i't be created so it will stop on the ). Cause it could also look like (CONMEBOL-GS) and then the range.length = 6 not work – user3258468 Feb 14 '14 at 08:40