views:

85

answers:

2

Hello, 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

+3  A: 

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.

Randy Simon
A: 

yeah, your idea is good. the case does not exist that there is no end but an start. but there can be more then one start and more then one end.

so this is why i would like to get an array of strings...in my sample text , there are also more than one "start" and "end"

maybe u can help me

Ploetzeneder
This should be added as a comment to my answer and then delete this "answer". We need to keep this thread clean so that others can read it.
Randy Simon