最近收集整理了一些输入是否为数字的几种方法
方法一:

static bool IsNumeric(string str)
{
if (str==null || str.Length==0)
return false;
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}



方法二:


private bool IsNumeric(string s)
{
char ch0 = '0';
char ch9 = '9';
for(int i=0; i < s.Length; i++)
{
if ((s[i] < ch0 || s[i] > ch9))
{
this.lblwarning.Text="此处应输入整数且非负!";
return false;
}
}
return true;
}


方法三:


static bool IsNumeric (string str)
{

System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
return reg1.IsMatch(str);
}



方法四:(可扩展)


public static bool IsConvert(string Expression,Type DataType)

{

switch(DataType.Name)

{

case "Double":

try

{

Double.Parse(Expression);

return true;

}

catch

{

return false;

}

case "DateTime":

try

{

DateTime.Parse(Expression);

return true;

}

catch

{

return false;

}

default:

return true;

}

}

相关文章:

  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
猜你喜欢
  • 2021-12-03
  • 2021-10-23
  • 2021-05-22
  • 2021-10-19
  • 2022-12-23
  • 2021-05-15
相关资源
相似解决方案