判断值是否为数字,此方法验证字符串中的每个字符,如果是数字,继续,如不是,返回false
public bool IsInteger(string strIn) { bool bolResult=true; if(strIn=="") { bolResult=false; } else { foreach(char Char in strIn) { if(char.IsNumber(Char)) continue; else { bolResult=false; break; } } } return bolResult; }
2. vb版
Public Class DataCheck '验证数字输入 Public Shared Function Check(ByVal checkObject As String) As Integer Dim result As Integer = 0 Dim strLen As Integer = checkObject.Length Dim i As Integer Dim baseStr As String baseStr = "0123456789" For i = 0 To strLen - 1 If baseStr.IndexOf(checkObject.Substring(i,1)) = -1 Then result = -1 Exit For Else result = baseStr.IndexOf(checkObject.Substring(i,1)) End If Next Return result End Function
3 综合(借鉴)
public static class Common { //isDigit public static bool isNumberic1(this string _string) { if (string.IsNullOrEmpty(_string)) return false; foreach (char c in _string) { if (!char.IsDigit(c)) return false; } return true; } //vb isnumberic public static bool isNumberic2(this string _string) { return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string); } //try parese public static bool isNumberic3(this string _string) { if (string.IsNullOrEmpty(_string)) return false; int i = 0; return int.TryParse(_string,out i); } //try catch public static bool isNumberic4(this string _string) { if (string.IsNullOrEmpty(_string)) return false; int i = 0; try { int.Parse(_string); } catch { return false; } return true; } //regex public static bool isNumberic5(this string _string) { return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string,"^//d+$"); } }