12元/年
1234万元
123元
456万元/年                      
4万元/月
546元/平米/天
45元/平米以上有好几个字符串,我想只得到前面的数字,后面的如万元/年分割出来不要,应该如何做,随机抽一个
所以没固定的位置可以取,我想不出来,不知道能不能实现,谢谢大家!

解决方案 »

  1.   

    仅取数字??这样吗:
            string objstr = "1234万元";
            string output = Regex.Replace(objstr, @"[^\d]", "");
            Response.Write(output); //取得数字~~~
    用javascript更方便
      

  2.   


    content ="456万元/年";
    Regex reg = new Regex("(?<text>^\d$)");
    Match match = reg.Match(content);
    while (match.Success)
    {
           Console.WriteLine(match.Groups["text"].Value);
           match = match.NextMatch();
    }
      

  3.   

    上面的几位办法不错,
    存在问题是, 1楼的办法, 如果是string objstr = "1234.98万元";则返回值:123498, 不知道能否修改一下正则表达式,让他保留小数点
    2楼的,我运行了一下,好像正则表达式也有问题.有时间,请测试一下. 不过我还是喜欢你们的简介的code.
     作为一个思路, 我的办法可以取出数据,包括小数点在内,就是代码多了点. 一下代码我已经运行通过, 没有问题, 若是发现任何异常,请告诉我.        string strNew;        public string GetNum(string str)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    if ( !Char.IsDigit(str,i) && str.Substring(i,1)!= ".")
                    {
                        return strNew;
                    }
                    else
                    {
                        strNew += str.Substring(i, 1);
                    }
                }
                return strNew;
            }
      

  4.   

    你的代码的目标是找到第一个不是数字或者.的字符位置,并截取它前面的字符串,下面的代码更清晰一些,也不需要成员变量:public string GetNum(string str)
            {
                int pos = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    if (!Char.IsDigit(str[i]) && (str[i] != '.'))
                    {
                        pos = i;
                        break;
                    }
                }            return str.Substring(0, pos);
            } 
      

  5.   

    大家都习惯于用正则表达式啦, 不过这个问题 用正则象是用杀猪用宰牛刀, 
    我的下面的方法 是不是更简单哪using Microsoft.VisualBasic;  这个名称空间跟其它空间一样, C#也可以用哦,不要看了VB 就以为是VB专用的了string a="129.98万元";
    decimal value=Conversion.Val(a);
      

  6.   

    string[] test = new string[] {"12元/年", "1234万元", "123元", "456万元/年", "4万元/月", "546元/平米/天", "45元/平米" };
    foreach (string s in test)
    {
        Console.WriteLine(System.Text.RegularExpressions.Regex.Match(s, @"^(?:[1-9][0-9]*|0)(\.[0-9]+)?").Value);
    }
    Console.ReadLine();
    /*输出:
    12
    1234
    123
    456
    4
    546
    45
    */
      

  7.   

    string str = "546元/平米/天 ";
                string temp = str.Substring(0, str.LastIndexOf('元'));
                Response.Write(temp);