views:

917

answers:

2

Is there a method built in to NSString that tokenizes the string and searches the beginning of each token? the compare method seems to only do the beginning of a string, and using rangeOfString isn't really sufficient because it doesn't have knowledge of tokens. Right now I'm thinking the best way to do this is to call

[myString componentsSeparatedByString:@" "]

and then loop over the resulting array, calling compare on each component of the string. Is this built-in and I just missed it?

+4  A: 

Using CFStringTokenizer for, um, tokenizing strings will be more robust than splitting on @" ", but searching the results is still left up to you.

Graham Lee
+2  A: 

You may want to look into RegexKit Lite:

http://regexkit.sourceforge.net/#RegexKitLite/

Although it's a third party library, it's basically a very small (one class) wrapper built around the built-in fairly powerful regular expression engine.

It seems like this would be more useful since you could have non-capturing expressions match around the token-separators and then the capturing portion include or not include the text you are looking for along with the remaining text between tokens. If you have not used regular expressions much before, you'll want to read some kind of reference but just be aware you can separate out matching patterns from content you want to see with a cryptic but very powerful syntax.

I'm also not sure you can use CFStringTokenizer on the iPhone since the iPhone specific doc set has no reference for it.

Kendall Helmstetter Gelner