Name.Text="1+1+1";
Response.Write(Name.Text);
如何能输出Response.Write "3"?
------解决方案--------------------------------------------------------
/// <summary>
/// 计算一个表达式的结果
/// </summary>
/// <param name= "expression "> 表达式 </param>
/// <returns> </returns>
public static object Eval(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
System.Data.DataColumn Col = new System.Data.DataColumn( "col1 ", typeof(string), expression);
table.Columns.Add(Col);
table.Rows.Add(new object[] { " " });
return table.Rows[0][0];
}
------解决方案--------------------------------------------------------
- C# code
string tmpStr = "1+1+1"; string []arrayStr = tmpStr.Split('+'); int sum = 0; for (int i = 0; i < arrayStr.Length; i++) { int j; int.TryParse(arrayStr[i],out j); sum += j; } Response.Write(sum);