views:

239

answers:

3

Hi,

How could I extract a part of an NSString before certain word(s):

For example:

If the keyword set of 'separators' is an NSArray of the following words "Series" and "documentary"

and if the NSStrings where

"Chain two Series documents" <---- I'd like to extract "Chain two"

"Chain documentary documents" <---- I'd like to extract "Chain"

Thanks

+1  A: 

See the NSString method -componentsSeparatedByString:

NSResponder
+3  A: 

You can use an NSScanner to scan strings up to the separators that you provide it.

Someone else mentioned NSString's -componentsSeparatedByString, but that would seem inefficient if you're unconcerned about the text after the separator or if you want to check for multiple separators, based on your example.

Preston
+1  A: 

Just use Regular Expressions. They are very useful and supported starting from 10.4 !

RegexKitLite

There is libicucore (libicu on Tiger).

So RegexKitLite is just wrapper for system library.

UncleMiF
So pattern for your task will look like:^(.*?)(?:Series|documentary)
UncleMiF