How to evaluate the string equation in ios

后端 未结 6 1265
一向
一向 2020-12-15 19:48

Is there way to solve the string equations in ios ?

For example

Input:

NSString * str =@\"1+2\";

Output:

<

6条回答
  •  无人及你
    2020-12-15 19:57

    What you can do is, You can get the components of the string separated by the sign "+" using this method and then you will get the array of components i.e. 1,2.

    NSArray* foo = [str componentsSeparatedByString: @"+"];

    int result = [[foo objectAtIndex:0] integerValue] + [[foo objectAtIndex:1] integerValue];

    and then you can use the integer value for those two elements and add them and store the result in an integer variable.

提交回复
热议问题