tags:

views:

31

answers:

1

i need to break a string into an array, as "2 + 3" should be as "2","+","3" even "2+3" should be "2","+","3"

+1  A: 

As long as the format is consistent (always a space between numbers and signs), NSArray's -componentsSeparatedByString: will work for you. If there's a possibility the string will appear like "2+3" or even "2 +3" you could try removing all whitespace characters with -stringByTrimmingCharactersInSet: then using -componentsSeparatedByCharactersInSet: with the sign characters you expect.

Joshua Nozzi
You could probably just use `componentsSeparatedByCharactersInSet:` by itself in most cases.
Chuck
Probably, but if you're looking for neat divisions with no spaces (ie, not " +" or "+ " but only "+" then you'll still have to get rid of the whitespace (before with one string is neater/easier than after with an array of strings).
Joshua Nozzi