在C# 中,用什么方法可以判断1段字符串是否全为数字?
例如: string str1 = "123456";string str2 = "123456asdfg";是不是用正规表达式啊?怎样写啊?
谢谢!

解决方案 »

  1.   

    private bool Check( string value)
    {
      try
      {
         Decimal a = System.Decimal.Parse(value) ;
         return true ;
      }
      catch
     {
     
    }
    }
      

  2.   

    private bool Check( string value)
    {
      try
      {
         Decimal a = System.Decimal.Parse(value) ;
         return true ;
      }
      catch
     {
        return false ;
     }
    }
    刚才没写完....按错键了.
      

  3.   

    正则:using System.Text.RegularExpressions; ......string str;......bool yn=Regex.IsMatch(str,@"^\d+$");
    if(yn==true) 
    { MessageBox.Show("y"); //含有数字
    }
    else
    {
    MessageBox.Show("n"); //不含有数字}
      

  4.   

    在属性生成器里拉出Regularexpressionvalidat,在ValidationExpression里添加  [0-9]+
      

  5.   

    bool check=Regex.IsMatch(str,@"^\d+$");