1

Is it possible to assign an int variable a value that is a result of expression written in a string? E.g. I have a string "5 - 3" and the expected result is 2.

tyumener
  • 51
  • 1
  • 8
  • possible duplicate of [in c#: expression evaluation function like flash script](http://stackoverflow.com/questions/534894/in-c-expression-evaluation-function-like-flash-script) – Henk Holterman Sep 28 '10 at 10:12
  • Possible duplicate of [Converting string expression to Integer Value](http://stackoverflow.com/questions/2607798/). – Dour High Arch May 09 '17 at 01:21

3 Answers3

4

You can use Calculator.NET

Giorgi
  • 29,755
  • 12
  • 86
  • 122
1

If you fancy writing your own (I did, it's fun!). Have a look on Wikipedia for:

Shunting Yard Algorithm and Reverse Polish Notation

The pseudo-code is really clear and it's a great learning experience

veggerby
  • 8,830
  • 2
  • 33
  • 43
RichK
  • 10,582
  • 6
  • 34
  • 48
-1

There is nothing built into the framework, so you can either built your own mathematical expression parser, or use one of the many libraries out there.

A search on ".net arithmetic parser" comes up with many results, some free, some not.

For instance:

Oded
  • 477,625
  • 97
  • 867
  • 998