C#判断一个string是否为数字

方案一:TryCatch(执行效率不高)
None.gif
private   bool  IsNumberic( string  oText)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif          
try
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif                  
int var1=Convert.ToInt32 (oText);
InBlock.gif                  
return true;
ExpandedSubBlockEnd.gif         }

InBlock.gif          
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif                  
return false;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif}

None.gif
None.gif方案二:正则表达式(推荐)
None.gifa)
None.gif
using  System;
None.gif
using  System.Text.RegularExpressions;
None.gif
None.gif
public   bool  IsNumber(String strNumber)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif           Regex objNotNumberPattern
=new Regex("[^0-9.-]");
InBlock.gif           Regex objTwoDotPattern
=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
InBlock.gif           Regex objTwoMinusPattern
=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
InBlock.gif           String strValidRealPattern
="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
InBlock.gif           String strValidIntegerPattern
="^([-]|[0-9])[0-9]*$";
InBlock.gif           Regex objNumberPattern 
=new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
InBlock.gif
InBlock.gif           
return !objNotNumberPattern.IsMatch(strNumber) &&
InBlock.gif                  
!objTwoDotPattern.IsMatch(strNumber) &&
InBlock.gif                  
!objTwoMinusPattern.IsMatch(strNumber) &&
InBlock.gif                  objNumberPattern.IsMatch(strNumber);
ExpandedBlockEnd.gif}

None.gif
None.gifb)
None.gif
public   static   bool  IsNumeric( string  value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif         
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
ExpandedBlockEnd.gif}

None.gif
public   static   bool  IsInt( string  value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif         
return Regex.IsMatch(value, @"^[+-]?\d*$");
ExpandedBlockEnd.gif}

None.gif
public   static   bool  IsUnsign( string  value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif         
return Regex.IsMatch(value, @"^\d*[.]?\d*$");
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif方案三:遍历
None.gifa)
None.gif
public   bool  isnumeric( string  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
char[] ch=new char[str.Length];
InBlock.gif    ch
=str.ToCharArray();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for(int i=0;i    dot.gif{
InBlock.gif        
if(ch[i]<48 || ch[i]>57)
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return true;
ExpandedBlockEnd.gif}

None.gif
None.gifb)
ExpandedBlockStart.gifContractedBlock.gif
public   bool  IsInteger( string  strIn)  dot.gif {
InBlock.gif        
bool bolResult=true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(strIn==""dot.gif{
InBlock.gif               bolResult
=false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif              
foreach(char Char in strIn) dot.gif{
InBlock.gif                         
if(char.IsNumber(Char))
InBlock.gif                                
continue;
ExpandedSubBlockStart.gifContractedSubBlock.gif                         
else dot.gif{
InBlock.gif                                 bolResult
=false;
InBlock.gif                                 
break;
ExpandedSubBlockEnd.gif                         }

ExpandedSubBlockEnd.gif              }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
return bolResult;
ExpandedBlockEnd.gif}

None.gif
None.gifc)
None.gif
public   static   bool  isNumeric( string  inString)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif        inString
=inString.Trim();
InBlock.gif        
bool haveNumber=false;
InBlock.gif        
bool haveDot=false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0;i        dot.gif{
InBlock.gif               
if (Char.IsNumber(inString[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                        haveNumber
=true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(inString[i]=='.')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                        
if (haveDot)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                                
return false;
ExpandedSubBlockEnd.gif                         }

InBlock.gif                         
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                         
dot.gif{
InBlock.gif                                haveDot
=true;
ExpandedSubBlockEnd.gif                          }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(i==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                          
if(inString[i]!='+'&&inString[i]!='-')
ExpandedSubBlockStart.gifContractedSubBlock.gif                          
dot.gif{
InBlock.gif                                  
return false;
ExpandedSubBlockEnd.gif                          }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                          
return false;
ExpandedSubBlockEnd.gif                 }

InBlock.gif                
if(i>20)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                          
return false;
ExpandedSubBlockEnd.gif                 }

ExpandedSubBlockEnd.gif         }

InBlock.gif         
return haveNumber;
ExpandedBlockEnd.gif       }

None.gif
None.gif
None.gif 
None.gif
None.gif方案四:改写vb的IsNumeric源代码(执行效率不高)
None.gif
None.gif
// 主调函数
None.gif
public   static   bool  IsNumeric( object  Expression)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
bool flag1;
InBlock.gif      IConvertible convertible1 
= null;
InBlock.gif      
if (Expression is IConvertible)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            convertible1 
= (IConvertible) Expression;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (convertible1 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
if (Expression is char[])
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  Expression 
= new string((char[]) Expression);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif      }

InBlock.gif      TypeCode code1 
= convertible1.GetTypeCode();
InBlock.gif      
if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
return Utils.IsNumericTypeCode(code1);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
string text1 = convertible1.ToString(null);
InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
long num2;
InBlock.gif            
if (!StringType.IsHexOrOctValue(text1, ref num2))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
double num1;
InBlock.gif                  
return DoubleType.TryParse(text1, ref num1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            flag1 
= true;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            flag1 
= false;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return flag1;
ExpandedBlockEnd.gif}

None.gif
None.gif
// 子函数
None.gif
//  return Utils.IsNumericTypeCode(code1);
None.gif
internal   static   bool  IsNumericTypeCode(TypeCode TypCode)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
switch (TypCode)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
case TypeCode.Boolean:
InBlock.gif            
case TypeCode.Byte:
InBlock.gif            
case TypeCode.Int16:
InBlock.gif            
case TypeCode.Int32:
InBlock.gif            
case TypeCode.Int64:
InBlock.gif            
case TypeCode.Single:
InBlock.gif            
case TypeCode.Double:
InBlock.gif            
case TypeCode.Decimal:
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
case TypeCode.Char:
InBlock.gif            
case TypeCode.SByte:
InBlock.gif            
case TypeCode.UInt16:
InBlock.gif            
case TypeCode.UInt32:
InBlock.gif            
case TypeCode.UInt64:
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
break;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif      }

InBlock.gif      
return false;
ExpandedBlockEnd.gif}

None.gif 
None.gif
None.gif
// -----------------
None.gif
// StringType.IsHexOrOctValue(text1, ref num2))
None.gif
internal   static   bool  IsHexOrOctValue( string  Value,  ref   long  i64Value)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
int num1;
InBlock.gif      
int num2 = Value.Length;
InBlock.gif      
while (num1 < num2)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
char ch1 = Value[num1];
InBlock.gif            
if (ch1 == '&')
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  ch1 
= char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
InBlock.gif                  
string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
InBlock.gif                  
if (ch1 == 'h')
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                        i64Value 
= Convert.ToInt64(text1, 0x10);
ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
else if (ch1 == 'o')
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                        i64Value 
= Convert.ToInt64(text1, 8);
ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                        
throw new FormatException();
ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if ((ch1 != ' '&& (ch1 != '\u3000'))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            num1
++;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return false;
ExpandedBlockEnd.gif}

None.gif
// ----------------------------------------------------
None.gif
//  DoubleType.TryParse(text1, ref num1);
None.gif
internal   static   bool  TryParse( string  Value,  ref   double  Result)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
bool flag1;
InBlock.gif      CultureInfo info1 
= Utils.GetCultureInfo();
InBlock.gif      NumberFormatInfo info3 
= info1.NumberFormat;
InBlock.gif      NumberFormatInfo info2 
= DecimalType.GetNormalizedNumberFormat(info3);
InBlock.gif      Value 
= StringType.ToHalfwidthNumbers(Value, info1);
InBlock.gif      
if (info3 == info2)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
return double.TryParse(Value, NumberStyles.Any, info2, out Result);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
try
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            Result 
= double.Parse(Value, NumberStyles.Any, info2);
InBlock.gif            flag1 
= true;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch (FormatException)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            flag1 
= double.TryParse(Value, NumberStyles.Any, info3, out Result);
ExpandedSubBlockEnd.gif      }

InBlock.gif      
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            flag1 
= false;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return flag1;
ExpandedBlockEnd.gif}

None.gif
None.gif方案五: 直接引用vb运行库(执行效率不高)
None.gif
None.gif方法: 首先需要添加Visualbasic.runtime的引用
None.gif 代码中Using Microsoft.visualbasic;
None.gif 程序中用Information.isnumeric(
" ddddd " ); 
None.gif

转载于:https://www.cnblogs.com/lin614/archive/2007/01/04/611222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值