I have a String
NSString *formula = @"base+unit1-unit2*unit3/unit4";
I am able to get all the words in an array using code :
NSArray *myWords = [formula componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"+-*/"]];
//myWords = {base , unit1,unit2,unit3,unit4}
but i am facing problem in getting all the operators in an array like this myOperators = {+,-,*,/} Please advice thanks
Use
NSScanner
to walk through the string extracting all of the parts you need (seescanCharactersFromSet:intoString:
andscanUpToCharactersFromSet:intoString:
).Would something like this work? (psudocode):
You could use NSRegularExpression to accomplish that. A quick solution could be:
you can build a mutable Array within that block with all the operators found according to the regular expression.
The regular expression ist just a quick example - you could create something more fancy than that :)
You can create a character set containing all characters except your operators using invertedSet
You'll then want to remove any empty strings from a mutable copy of the array