日期:2014-05-18  浏览次数:20731 次

怎么判断输入的字符串是不是由数字组成的?
怎么判断输入的字符串是不是由数字组成的?

------解决方案--------------------
private bool IsMatch()//你可以把teststr作为参数 private bool IsMatch(string teststr)
{
bool issucess = false;
string teststr = "3534fd-sads()";
Regex regs = new Regex(@"^\d+$");
if (regs.IsMatch(teststr))
{
issucess = true;
}
else
{
issucess = false;
}
return issucess;
}
------解决方案--------------------
用try{} catch{} 
try
{
int.Parse(string);
return true;
}
catch
{
return false;


或者用正则表达式

C# code

protected bool isNumberic(string message,out int result)
{
        System.Text.RegularExpressions.Regex rex=
        new System.Text.RegularExpressions.Regex(@"^\d+$");
        result = -1;
        if (rex.IsMatch(message))
        {
            result = int.Parse(message);
            return true;
        }
        else
            return false;
}

------解决方案--------------------
用正则表达式