我正在做一个计算器,无法在对应于操作数的情况下分隔输入字符串。
例如:2 * 5-6 +8/2。我想要一个包含2、5、6、8、2组成部分的数组,这样我也可以存储操作者,然后进行相应的排序。请帮忙

最佳答案

NSString *str=@"2*5 - 6 +8/2";  // assume that this is your str

// here remove the white space
str =[str stringByReplacingOccurrencesOfString:@" " withString:@""];
// here remove the all special characters in NSString
NSCharacterSet *noneedstr = [NSCharacterSet characterSetWithCharactersInString:@"*/-+."];
str = [[str componentsSeparatedByCharactersInSet: noneedstr] componentsJoinedByString:@","];
 NSLog(@"the str=-=%@",str);
输出为
 the str=-=2,5,6,8,2

关于ios - 用特殊字符分隔字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26686470/

10-13 04:05
查看更多