Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString tokenize in Objective-C

Found this at http://borkware.com/quickies/one?topic=NSString (useful link):

NSString *string = @"oop:ack:bork:greeble:ponies";
NSArray *chunks = [string componentsSeparatedByString: @":"];

Hope this helps!

Adam


Everyone has mentioned componentsSeparatedByString: but you can also use CFStringTokenizer (remember that an NSString and CFString are interchangeable) which will tokenize natural languages too (like Chinese/Japanese which don't split words on spaces).


If you just want to split a string, use -[NSString componentsSeparatedByString:]. For more complex tokenization, use the NSScanner class.


If your tokenization needs are more complex, check out my open source Cocoa String tokenizing/parsing toolkit: ParseKit:

http://parsekit.com

For simple splitting of strings using a delimiter char (like ':'), ParseKit would definitely be overkill. But again, for complex tokenization needs, ParseKit is extremely powerful/flexible.

Also see the ParseKit Tokenization documentation.