我在 c# 中有一个可以进行数学运算的 winform 项目。字符串就像"=B10+B4*(B12-B8)"
. 并B10
代表“3”,B4
代表“10” ,B12
代表“6”,B8
代表“2”。我想将此字符串转换为"=3+10*(6-2)"
. 所以可以进行数学运算。对于解决方案,我创建了三个列表。我成功地制作了 operatorList 和 numberRepresentList ,如下所示。我的问题是将这两个列表与特定顺序结合起来。
List<string> operatorList= new List<string>();
List<string> numberRepresentList = new List<string>();
List<string> mergedList= new List<string>();
operatorList={"=","+","*","(","-",")"}
numberRepresentList =={"3","10","6","2"}
我如何将它们与特定顺序结合起来,结果将显示为:
mergedList={"=","3","+","10","*","(","6","-","2",")"}
我在表格中搜索过,但找不到类似的。所以我创造了一个。提前谢谢。
编辑:感谢@vivek nuna。我做了一个列表名称 partOfString
comesFromUser="=B10+B4*(B12-B8)"
partofString={"B10","B4","B12","B8"}
numberRepresentList =={"3","10","6","2"}
for (int i = 0; i < partOfString.Count; i++)
{
comesFromUser = comesFromUser.Replace(partOfString[i], numberRepresentList[i]);
}