-2

I trying to calculate my string but I don't have any idea how to do that.

I want to do something like this:

string input = "2+2+2+2*5^2/2";
Console.WriteLine(Calculate(input));

I tried to use DataTable().Compute() but its dose not support many of operators.

  • 2
    Does this answer your question? [Evaluating string "3\*(4+2)" yield int 18](https://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18) adn [String math evaluator in .NET?](https://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) and [Arithmetic expression in a string](https://stackoverflow.com/questions/4620437/evaluate-an-arithmetic-expression-stored-in-a-string-c) and [Convert a string to a mathematical expression programmatically](https://stackoverflow.com/questions/21750824/how-to-convert-a-string-to-a-mathematical-expression-programmatically) –  May 30 '21 at 10:03
  • here is already solved similar issues on this [LINK](https://stackoverflow.com/questions/234217/is-it-possible-to-compile-and-execute-new-code-at-runtime-in-net) – Pashyant Srivastava May 30 '21 at 12:07

1 Answers1

0

You can use DataTable.Compute for that. this method computes the given expression on the current rows that pass the filter criteria.

You can read more about it here: https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable.compute?view=net-5.0

string input = "2+2+2+2*5^2/2";
string res = new DataTable().Compute(input, null).ToString();

Console.WriteLine(res);
Ran Turner
  • 14,906
  • 5
  • 47
  • 53