-4

i would like to know if there is a way to conver a string + to a real plus. Example

string bla = "4+2";

i would like to reach, 4 + 2 = 6

Thanks!

Raging Bull
  • 18,593
  • 13
  • 50
  • 55

4 Answers4

2

Try this

using System.Data; //refference for DataTable 

string bla = "4+2";
object value = new DataTable().Compute(bla , null);
string res=value.ToString();//res=6
0

you can compile the expression and evaluate it at runtime. take a look here: http://www.codeproject.com/Articles/3988/Evaluating-Mathematical-Expressions-by-Compiling-C

Z .
  • 12,657
  • 1
  • 31
  • 56
0

Try Following Code

string bla = "4+2";
Expression exp = new Expression(bla);
Console.write(exp.Evaluate());
Kamlesh
  • 511
  • 1
  • 7
  • 18
-3
Int32 Number1; 
Int32 Number2;
String Result = Convert.ToString(Number1+Number2);
SueSaya
  • 57
  • 1
  • 1
  • 9