• ASP.NET中将字符串转换成bool类型


    使用方法一要 添加com组件引用

     Interop.MSScriptControl.dll

    页面代码:

    protected void Page_Load(object sender, EventArgs e)
        {
            string str_Expression1 = "3>1 and 3>2 and 6>=8";
             Response.Write(StringToBoll2(str_Expression1));

            string str_Expression2 = "8>6 and 4>7 and 9>8";
            Response.Write(StringToBoll1(str_Expression2));

            Response.End();
        }


        /// <summary>
        /// 方法一:将字符串转换为BOOL
        /// </summary>
        /// <param name="Expression">表达式</param>
        /// <returns></returns>
        public string StringToBoll1(string Expression)
        {
            string strResult = null;

            try
            {
                MSScriptControl.ScriptControlClass script = new MSScriptControl.ScriptControlClass();
                script.Language = "VBscript";
                strResult = script.Eval(Expression).ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            return strResult;
        }


        /// <summary>
        /// 方法二:将字符串转换为BOOL
        /// </summary>
        /// <param name="Expression">表达式</param>
        /// <returns></returns>
        public string StringToBoll2(string Expression)
        {
            DataTable dt = new DataTable();
            return (dt.Compute(Expression, "").ToString());
        }
    }

  • 相关阅读:
    【算法笔记】B1015 德才论
    【算法笔记】B1014 福尔摩斯的约会
    【算法笔记】B1013 数素数
    【算法笔记】B1012 数字分类
    【算法笔记】B1011 A+B 和 C
    【算法笔记】B1010 一元多项式求导
    【算法笔记】B1009 说反话
    【算法笔记】B1008 数组元素循环右移问题
    SSLOJ 1336.膜拜神牛
    SSLOJ 1335.蛋糕切割
  • 原文地址:https://www.cnblogs.com/Cynosure/p/2305901.html
Copyright © 2020-2023  润新知