如何替换二维字符串数组中的值

时间:2016-11-18 11:01:17

标签: c# string multidimensional-array

如何执行此任务?

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[]permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            // For camera
            case REQUEST_CAMERA:
                if (grantResults.length > 0) {
                    boolean permGiven = true;
                    for(int i=0; i<grantResults.length; i++){
                        if(grantResults[i] != PackageManager.PERMISSION_GRANTED)
                            permGiven = false;
                    }
                    if(permGiven)
                        OpenCamera();
                    else
                        Toast.makeText(MainActivity.this, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
                }
                break;


        }
    }

现在,我拆分了BasedOn字符串

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

下一步该怎么做?请帮帮我。

5 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:0)

您可以查看DataTable.Compute方法。 它可以像你这样使用:

using System.Data;

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

替换&#34; x&#34;和&#34; y&#34;使用数字值,您可以使用string.Replace

答案 2 :(得分:0)

如果要替换字符串并计算结果,可以执行以下操作:

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)。

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

使用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)