C#判斷一串字符串是否為數字字符串
簡介:
當現在有一個字符串,需要判斷這個字符串是否可以轉為Number類型時,可以運用下面這段代碼進行判別。其中包括判別帶小數點的數字字符串。
public bool IsNumeric(string str) { if (str == null || str.Length == 0) return false; ASCIIEncoding ascii = new ASCIIEncoding(); byte[] bytestr = ascii.GetBytes(str); foreach (byte c in bytestr) { if (c!=46)//小數點的ASCII碼為46 { if (c < 48 || c > 57)//非0~9的ASCII碼 { return false; } } else { return true; } } return true; }