计算字符串“3*(4+2)"产量 int 18 [英] Evaluating string "3*(4+2)" yield int 18

查看:18
本文介绍了计算字符串“3*(4+2)"产量 int 18的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 框架中是否有可以计算字符串中包含的数值表达式并返回结果的函数?例如:

Is there a function the .NET framework that can evaluate a numeric expression contained in a string and return the result? F.e.:

string mystring = "3*(2+4)";
int result = EvaluateExpression(mystring);
Console.Writeln(result); // Outputs 18

是否有标准框架函数可以替换我的 EvaluateExpression 方法?

Is there a standard framework function that you can replace my EvaluateExpression method with?

推荐答案

如果要计算字符串表达式,请使用以下代码片段.

If you want to evaluate a string expression use the below code snippet.

using System.Data;

DataTable dt = new DataTable();
var v = dt.Compute("3 * (2+4)","");

这篇关于计算字符串“3*(4+2)"产量 int 18的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆