【问题标题】:How to replace values in 2-D string array如何替换二维字符串数组中的值
【发布时间】:2016-11-18 11:01:17
【问题描述】:

如何执行此任务?

int Amount, x=1000,y=200;
string BasedOn="x*12/100+y*5/100";
//In place of x and y in BasedOn I want to  replace with x,y values like (1000*12%+200*5%) 
//and the calculated result(130) should be assigned to Amount variable

现在,我拆分了 BasedOn 字符串

string[][] strings = BasedOn
    .Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries)
    .Select(w => w.Split('*').ToArray())
    .ToArray();

接下来要做什么?请帮帮我。

【问题讨论】:

    标签: c# string multidimensional-array


    【解决方案1】:

    我让你的代码更灵活

        static void Main(string[] args)
        {
            Dictionary<string, object> variables = new Dictionary<string, object>();
            variables.Add("x", 1000);
            variables.Add("y", 200);
            string equation = "x*12/100+y*5/100";
            var result = Calculate(variables, equation);
        }
        static object Calculate(Dictionary<string, object> variables, string equation)
        {
            variables.ToList().ForEach(v => equation = equation.Replace(v.Key, v.Value.ToString()));
            return new DataTable().Compute(equation, string.Empty);
        }
    

    【讨论】:

      【解决方案2】:

      您可以查看 DataTable.Compute 方法。 在您的情况下可以这样使用:

      using System.Data;
      
      DataTable dt = new DataTable();
      var Amount = dt.Compute("1000*12%+200*5%","");
      

      要使用数值替换“x”和“y”,您可以使用 string.Replace

      【讨论】:

        【解决方案3】:

        看看 NCalc (http://ncalc.codeplex.com/)

        Expression e = new Expression("2 + 3 * 5");
        Debug.Assert(17 == e.Evaluate());
        

        【讨论】:

          【解决方案4】:

          如果你想替换字符串并计算结果,你可以这样做:

          int Amount, x=1000,y=200;
          string BasedOn=$"{x}*12/100+{y}*5/100";
          
          DataTable dt = new DataTable();
          var v = dt.Compute(BasedOn,"");
          

          v 将是您的结果 (130)。

          编辑:你必须用除以 100 替换你的 %,因为数据表认为它是一个 mod 运算符。

          【讨论】:

          • @mybirthname 已修复。
          • @UmutSeven 在 var v = dt.Comput(BasedOn,"") 处得到 SyntaxExceptionError(无法解释位置 2 处的令牌 '"')
          • @snehanagaruru 这很奇怪,它在我的机器上工作。试试string.Empty 而不是双引号 ("")?
          【解决方案5】:

          DataColumn.Expression:

          int Amount, x = 1000, y = 200; string BasedOn = "x*12%+y*5%";
          
          var dt = new DataTable();
          dt.Columns.Add("x", typeof(int));   // in Visual Studio 2015 you can use nameof(x) instead of "x"
          dt.Columns.Add("y", typeof(int));
          dt.Columns.Add("Amount", typeof(int), BasedOn.Replace("%", "/100")); // "x*12/100+y*5/100" ("%" is considered modulus operator)
          
          Amount = (int)dt.Rows.Add(x, y)["Amount"];  // 130
          

          使用RegEx.ReplaceDataTable.Compute

          string expression = Regex.Replace(BasedOn.Replace("%", "/100"), "[a-zA-Z]+",
              m => (m.Value == "x") ? x + "" : (m.Value == "y") ? y + "" : m.Value);  // "1000*12/100+200*5/100"
          
          Amount = (int)(double)new DataTable().Compute(expression, ""); // 130.0 (double)
          

          【讨论】:

            猜你喜欢
            • 2017-06-09
            • 2021-01-28
            • 2019-01-23
            • 2017-09-20
            • 1970-01-01
            • 2018-08-08
            • 1970-01-01
            • 1970-01-01
            • 2015-01-21
            相关资源
            最近更新 更多