本文介紹一種通過正則表達式來判斷輸入的內(nèi)容是否為數(shù)字的方法,具體如下。
用正則表達式來驗證字符串是否為數(shù)字字符串要用到Regex類的isMatch()方法。該類在System.Text.RegularExpressions命名空間中; 您可以通過using System.Text.RegularExpressions;導入命名空間來訪問Regex類。也可以直接通過System.Text.RegularExpressions.Regex 來訪問。
下面編寫一個判斷是否為數(shù)字字符串的方法:
protected bool isNumberic(string str,out int result)
{
System.Text.RegularExpressions.Regex rex=
new System.Text.RegularExpressions.Regex(@"^/d+$");
result = -1;
if (rex.IsMatch(str))
{
result = int.Parse(str);
return true;
}
else
return false;
}
新聞熱點
疑難解答
圖片精選