方法一】:使用 try{} catch{} 语句。
      我们可以在try语句块中试图将string类型的字符串变量转换为int类型,如果该字符串不是数字字符串则会抛出异常,这时在catch语句块中就能捕获异常。一旦发现异常,则不是数字字符串。

    try

        {
            result = Convert.ToInt32(message);    
            return true;
        }
        catch
        {
            return false;
        }

方法二】:通过正则表达式来判断。

通过using System.Text.RegularExpressions;导入命名空间来访问Regex类。也可以直接通过System.Text.RegularExpressions.Regex来访问。

System.Text.RegularExpressions.Regex rex=

new System.Text.RegularExpressions.Regex(@"^\d+$");
       if (rex.IsMatch(message))
        {
        }
        else
        {
        }
  

 

posted on 2013-09-09 16:59  清风暮雨  阅读(226)  评论(0)    收藏  举报