Ploetzeneder
Ploetzeneder

Reputation: 1321

How can i get parts of a String in Objective-C?

i would like to fetch a part of a string in objective C: a sampletext: This is the story of a startnew mountainend. There were many bluestart green houses end.........

the function should return a array of strings which are all in between the "start" and the "end".

How can i write this in objective C?

Andreas

Upvotes: 0

Views: 862

Answers (1)

Randy Simon
Randy Simon

Reputation: 3324

I think what you want is something like this.

    NSString *text = nil;

    NSScanner *theScanner = [NSScanner scannerWithString:@"This is the story of a startnew mountainend. There were many bluestart green houses end"];

    [theScanner scanUpToString:@"start" intoString:NULL] ; 

    [theScanner scanUpToString:@"end" intoString:&text] ;

Of course there are several edge cases you should watch out for like what if you reach the end of the string without find "end"? What if there is a "start" after you already found the word "start" before you find an "end"? Anyways, hopefully this points you in the right direction.

Upvotes: 4

Related Questions