views:

127

answers:

1

I would like to separate my string by spaces, commas, periods (ie. punctuations). I am using:

[myString componentsSeparatedByString:@" "];

to separate by spaces, but need to be able to split by punctuations as well.

+4  A: 

Use componentsSeparatedByCharactersInSet: with a character set made up of whatever characters you want to split by.

Chuck
Thanks I tried: [NSCharacterSet characterSetWithCharactersInString:@" ,.?!"];and that did the trick
Sheehan Alam
Sheehan Alam: The space character is not the only whitespace character, and those four are not the only punctuation characters. Start with the `whitespaceAndNewlineCharacterSet`, make a mutable copy (and don't forget to release or autorelease it), and union in the `punctuationCharacterSet`. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableCharacterSet_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableCharacterSet/formUnionWithCharacterSet:
Peter Hosey