2

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 Answers1

1

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
  • 60,946
  • 14
  • 140
  • 135
  • You could probably just use `componentsSeparatedByCharactersInSet:` by itself in most cases. – Chuck Sep 01 '10 at 16:56
  • 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 Sep 01 '10 at 16:58