方法一:
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)
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="此處應(yīng)輸入整數(shù)且非負(fù)!";
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);
}
方法四:(可擴(kuò)展)
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;
}
}
c#驗證輸入的是否數(shù)字的方法
其實用正則表達(dá)式也可以
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;
}
正則表達(dá)的寫法是:
static bool isnumeric(string str)
{
system.text.regularexpressions.regex reg1
= new system.text.regularexpressions.regex(@"^[-]?/d+[.]?/d*$");
return reg1.ismatch(str);
}
新聞熱點
疑難解答
圖片精選