Cy.
Cy.

Reputation: 2145

Extract part of a NSString in Cocoa in an efficient way

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

Upvotes: 1

Views: 871

Answers (3)

UncleMiF
UncleMiF

Reputation: 1171

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.

Upvotes: 1

user155959
user155959

Reputation:

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.

Upvotes: 4

NSResponder
NSResponder

Reputation: 16861

See the NSString method -componentsSeparatedByString:

Upvotes: 1

Related Questions