方法一:遍历
a)
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;
}
... {
if (str==null || str.Length==0)
return false;
foreach(char c in str)
...{
if (!Char.IsNumber(c))
...{
return false;
}
}
return true;
}
b)
public
bool
isnumeric(
string
str)
... {
char[] ch=new char[str.Length];
ch=str.ToCharArray();
for(int i=0;i<ch.Length;i++)
...{
if(ch[i]<48 || ch[i]>57)
return false;
}
return true;
}
... {
char[] ch=new char[str.Length];
ch=str.ToCharArray();
for(int i=0;i<ch.Length;i++)
...{
if(ch[i]<48 || ch[i]>57)
return false;
}
return true;
}
c)
public
static
bool
isNumeric(
string
inString)
... {
inString=inString.Trim();
bool haveNumber=false;
bool haveDot=false;
for(int i=0;i<inString.Length;i++)
...{
if (Char.IsNumber(inString[i]))
...{
haveNumber=true;
}
else if(inString[i]=='.')
...{
if (haveDot)
...{
return false;
}
else
...{
haveDot=true;
}
}
else if(i==0)
...{
if(inString[i]!='+'&&inString[i]!='-')
...{
return false;
}
}
else
...{
return false;
}
if(i>20)
...{
return false;
}
}
return haveNumber;
}
}
... {
inString=inString.Trim();
bool haveNumber=false;
bool haveDot=false;
for(int i=0;i<inString.Length;i++)
...{
if (Char.IsNumber(inString[i]))
...{
haveNumber=true;
}
else if(inString[i]=='.')
...{
if (haveDot)
...{
return false;
}
else
...{
haveDot=true;
}
}
else if(i==0)
...{
if(inString[i]!='+'&&inString[i]!='-')
...{
return false;
}
}
else
...{
return false;
}
if(i>20)
...{
return false;
}
}
return haveNumber;
}
}
方法二:正则表达式
a)
static
bool
IsNumeric(
string
str)
... {
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?d+[.]?d*$");
return reg1.IsMatch(str);
}
... {
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?d+[.]?d*$");
return reg1.IsMatch(str);
}
b)
using
System;
using System.Text.RegularExpressions;
public bool IsNumber(String strNumber)
... {
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
using System.Text.RegularExpressions;
public bool IsNumber(String strNumber)
... {
Regex objNotNumberPattern=new Regex("[^0-9.-]");
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
c)
public
static
bool
IsNumeric(
string
value)
... {
return Regex.IsMatch(value, @"^[+-]?d*[.]?d*$");
}
public static bool IsInt( string value)
... {
return Regex.IsMatch(value, @"^[+-]?d*$");
}
public static bool IsUnsign( string value)
... {
return Regex.IsMatch(value, @"^d*[.]?d*$");
}
... {
return Regex.IsMatch(value, @"^[+-]?d*[.]?d*$");
}
public static bool IsInt( string value)
... {
return Regex.IsMatch(value, @"^[+-]?d*$");
}
public static bool IsUnsign( string value)
... {
return Regex.IsMatch(value, @"^d*[.]?d*$");
}
方法三:Try...Catch(执行效率不高)
/**/
/// Try...Catch(执行效率不高)
/// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
/// <param name="oText"></param>
/// <returns></returns>
private bool IsNumberic( string oText)
... {
try
...{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
...{
return false;
}
}
/// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
/// <param name="oText"></param>
/// <returns></returns>
private bool IsNumberic( string oText)
... {
try
...{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
...{
return false;
}
}