怎么提取这样的字符串12*36=2 
分别提取 12 和 36 还有 2出来

解决方案 »

  1.   

    string a="12*36=2";
    string[] b=a.Split('*');
    string[] c=b.Split('=');
      

  2.   

    string a="12*36=2"; 
    string[] b=a.Split('*'); 
    string[] c=b.Split('=');上面这种方式可以实现也可以用正则来实现
      

  3.   

    string exp = "12 * 36 = 2";
    Regex reg = new Regex(@"\d+");
    MatchCollection matches = reg.Matches(exp);
    // matches[0].Value 为 12
    // matches[1].Value 为 36
    // matches[2].Value 为 2
    用正则好一点,加减乘除都可以,数字前后有几个空格也没关系,包含多个超过三个数字的运算式也可以依次获取到其余的数字。
      

  4.   

    12*36=2 ??哪来的?
    string exp = "12 * 36 = 2";
    Regex reg = new Regex(@"\d+");
    MatchCollection matches = reg.Matches(exp);
    // matches[0].Value 为 12
    // matches[1].Value 为 36
    // matches[2].Value 为 2
      

  5.   

            String str = "12 * 36 = 2";
            Regex objRegex = new Regex(@"\s*(?<First>\d+)\s*\*\s*(?<Second>\d+)\s*=\s*(?<Third>\d+)\s*");        MatchCollection objCollection = objRegex.Matches(str);
            for (int i = 0; i < objCollection.Count; i++)
            {
                Response.Write(
                    "第一个:" + objCollection[i].Groups["First"].Value +
                    "第二个:" + objCollection[i].Groups["Second"].Value +
                    "第三个:" + objCollection[i].Groups["Third"].Value + "<br/>");
            }        ////结果为:
            ////第一个:12第二个:36第三个:2