C# 解析 j s 三元运算符

   private void button1_Click(object sender, EventArgs e)
        {
            //转换

            string str1 = "表达式1?表达式2:表达式3";
            str1.IndexOf('?'); //查找第一个?所在位置
            StringBuilder sb = new StringBuilder();
            string[] str1s = str1.Split('?');
            str1s[0] = "表达式1";//表达式1 ,
            string[] str1ss = str1s[1].Split(':');
            str1ss[0] = "表达式2";
            str1ss[1] = "表达式3";
            sb.AppendFormat("if({0})", str1s[0]);
            sb.Append("\r\n");
            sb.Append("{");
            sb.Append("\r\n");
            sb.Append("  " + str1ss[0]);
            sb.Append("\r\n");
            sb.Append("}");
            sb.Append("\r\nelse\r\n");
            sb.Append("{");
            sb.Append("\r\n");
            sb.Append("  " + str1ss[1]);
            sb.Append("\r\n");
            sb.Append("}");
            textBox1.Text += sb.ToString();
            ff();

        }
        public void ff()
        {
            string str1 = "表达式1?表达式2:表达式3?表达式4:表达式5";
            str1 = "\"function\" == typeof define && define.amd ? define(\"moxie\", [], i) : \"object\" == typeof module && module.exports ? module.exports = i() : e.moxie = i()";
            string[] str1s = str1.Split('?');
            //str1s[0] = "表达式1";
            //str1s[1].Split(':')[0] = "表达式2";
            //str1s[1].Split(':')[1] = "表达式3";
            //str1s[2].Split(':')[0] = "表达式4";
            //str1s[2].Split(':')[1] = "表达式5";
            StringBuilder sb = new StringBuilder();
            sb.Append("\r\n");
            sb.AppendFormat("if({0})", str1s[0]); //追加表达式1
            sb.Append("\r\n");
            sb.Append("{");
            sb.Append("\r\n"); //追加左括号
            sb.Append("  " + str1s[1].Split(':')[0]);//表达式2
            sb.Append("\r\n");//追加右括号
            sb.Append("}");
            sb.AppendFormat("\r\nelse if({0})\r\n", str1s[1].Split(':')[1]);
            sb.Append("{"); sb.Append("\r\n");
            sb.Append("  " + str1s[2].Split(':')[0]);//表达式4
            sb.Append("}"); sb.Append("\r\n");
            sb.Append("\r\nelse\r\n");
            sb.Append("{");
            sb.Append("\r\n"); //追加左括号
            sb.Append("  " + str1s[2].Split(':')[1]);//表达式5
            sb.Append("\r\n");//追加右括号
            sb.Append("}");
            textBox1.Text += sb.ToString();
        }

 

posted @ 2018-06-04 23:30  enych  阅读(476)  评论(0编辑  收藏  举报