有幾種方法來測試只有數字編號:
首先,從來沒有使用Int
因爲它的最大值,要麼使用int
或Int32
。
解析
int result;
if (int.TryParse("123", out result))
{
Debug.WriteLine("Valid integer: " + result);
}
else
{
Debug.WriteLine("Not a valid integer");
}
Convert.ToInt32()
// throws ArgumentNullExceptionint
result1 = Int32.Parse(null);
// doesn't throw an exception, returns 0
int result2 = Convert.ToInt32(null);
則IsNumeric()
using Microsoft.VisualBasic;
// ......
bool result = Information.IsNumeric("123");
模式匹配
string strToTest = "123";
Regex reNum = new Regex(@"^\d+$");
bool isNumeric = reNum.Match(strToTest).Success;
,什麼是你已經嘗試了什麼問題? – 2012-03-13 08:26:45
我不會寫做** **究竟什麼'int.TryParse'呢... – gdoron 2012-03-13 08:31:10
其實沒有什麼錯都在那邊,我只是想和大家分享的TryParse的結果的方法。 – Hoger 2012-03-13 08:34:14