• C#判断一个string是否为数字(转载)


      1 案一:Try...Catch(执行效率不高)
      2 private bool IsNumberic(string oText)
      3 {
      4           try
      5          {
      6                   int var1=Convert.ToInt32 (oText);
      7                   return true;
      8          }
      9           catch
     10          {
     11                   return false;
     12          }
     13 }
     14 
     15 方案二:正则表达式(推荐)
     16 a)
     17 public static bool IsNumeric(string value)
     18 {
     19          return Regex.IsMatch(value, @"^[+-]?d*[.]?d*$");
     20 }
     21 public static bool IsInt(string value)
     22 {
     23          return Regex.IsMatch(value, @"^[+-]?d*$");
     24 }
     25 public static bool IsUnsign(string value)
     26 {
     27          return Regex.IsMatch(value, @"^d*[.]?d*$");
     28 }
     29  public static bool isTel(string strInput)
     30 {
     31         return Regex.IsMatch(strInput, @"d{3}-d{8}|d{4}-d{7}");
     32 }
     33 b)
     34 using System;
     35 using System.Text.RegularExpressions;
     36 public bool IsNumber(String strNumber)
     37 {
     38            Regex objNotNumberPattern=new Regex("[^0-9.-]");
     39            Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
     40            Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
     41            String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
     42            String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
     43            Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
     44            return !objNotNumberPattern.IsMatch(strNumber) &&
     45                   !objTwoDotPattern.IsMatch(strNumber) &&
     46                   !objTwoMinusPattern.IsMatch(strNumber) &&
     47                   objNumberPattern.IsMatch(strNumber);
     48 }
     49 
     50 
     51 方案三:遍历
     52 a)
     53 public bool isnumeric(string str)
     54 {
     55     char[] ch=new char[str.Length];
     56     ch=str.ToCharArray();
     57     for(int i=0;i    {
     58         if(ch[i]<48 || ch[i]>57)
     59             return false;
     60     }
     61     return true;
     62 }
     63 
     64 b)
     65 public bool IsInteger(string strIn) {
     66         bool bolResult=true;
     67         if(strIn=="") {
     68                bolResult=false;
     69         }
     70         else {
     71               foreach(char Char in strIn) {
     72                          if(char.IsNumber(Char))
     73                                 continue;
     74                          else {
     75                                  bolResult=false;
     76                                  break;
     77                          }
     78               }
     79         }
     80         return bolResult;
     81 }
     82 c)
     83 
     84 public static bool isNumeric(string inString)
     85 {
     86         inString=inString.Trim();
     87         bool haveNumber=false;
     88         bool haveDot=false;
     89         for(int i=0;i        {
     90                if (Char.IsNumber(inString[i]))
     91                 {
     92                         haveNumber=true;
     93                 }
     94                 else if(inString[i]=='.')
     95                 {
     96                         if (haveDot)
     97                         {
     98                                 return false;
     99                          }
    100                          else
    101                          {
    102                                 haveDot=true;
    103                           }
    104                 }
    105                 else if(i==0)
    106                 {
    107                           if(inString[i]!='+'&&inString[i]!='-')
    108                           {
    109                                   return false;
    110                           }
    111                 }
    112                 else
    113                 {
    114                           return false;
    115                  }
    116                 if(i>20)
    117                 {
    118                           return false;
    119                  }
    120          }
    121          return haveNumber;
    122        }
    123 
    124 
    125 方案四:改写vb的IsNumeric源代码(执行效率不高)
    126 //主调函数
    127 public static bool IsNumeric(object Expression)
    128 {
    129       bool flag1;
    130       IConvertible convertible1 = null;
    131       if (Expression is IConvertible)
    132       {
    133             convertible1 = (IConvertible) Expression;
    134       }
    135       if (convertible1 == null)
    136       {
    137             if (Expression is char[])
    138             {
    139                   Expression = new string((char[]) Expression);
    140             }
    141             else
    142             {
    143                   return false;
    144             }
    145       }
    146       TypeCode code1 = convertible1.GetTypeCode();
    147       if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
    148       {
    149             return Utils.IsNumericTypeCode(code1);
    150       }
    151       string text1 = convertible1.ToString(null);
    152       try
    153       {
    154             long num2;
    155             if (!StringType.IsHexOrOctValue(text1, ref num2))
    156             {
    157                   double num1;
    158                   return DoubleType.TryParse(text1, ref num1);
    159             }
    160             flag1 = true;
    161       }
    162       catch (Exception)
    163       {
    164             flag1 = false;
    165       }
    166       return flag1;
    167 }//子函数
    168 // return Utils.IsNumericTypeCode(code1);
    169 internal static bool IsNumericTypeCode(TypeCode TypCode)
    170 {
    171       switch (TypCode)
    172       {
    173             case TypeCode.Boolean:
    174             case TypeCode.Byte:
    175             case TypeCode.Int16:
    176             case TypeCode.Int32:
    177             case TypeCode.Int64:
    178             case TypeCode.Single:
    179             case TypeCode.Double:
    180             case TypeCode.Decimal:
    181             {
    182                   return true;
    183             }
    184             case TypeCode.Char:
    185             case TypeCode.SByte:
    186             case TypeCode.UInt16:
    187             case TypeCode.UInt32:
    188             case TypeCode.UInt64:
    189             {
    190                   break;
    191             }
    192       }
    193       return false;
    194 }
    195 
    196 //-----------------
    197 //StringType.IsHexOrOctValue(text1, ref num2))
    198 internal static bool IsHexOrOctValue(string Value, ref long i64Value)
    199 {
    200       int num1;
    201       int num2 = Value.Length;
    202       while (num1 < num2)
    203       {
    204             char ch1 = Value[num1];
    205             if (ch1 == '&')
    206             {
    207                   ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
    208                   string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
    209                   if (ch1 == 'h')
    210                   {
    211                         i64Value = Convert.ToInt64(text1, 0x10);
    212                   }
    213                   else if (ch1 == 'o')
    214                   {
    215                         i64Value = Convert.ToInt64(text1, 8);
    216                   }
    217                   else
    218                   {
    219                         throw new FormatException();
    220                   }
    221                   return true;
    222             }
    223             if ((ch1 != ' ') && (ch1 != 'u3000'))
    224             {
    225                   return false;
    226             }
    227             num1++;
    228       }
    229       return false;
    230 }
    231 //----------------------------------------------------
    232 // DoubleType.TryParse(text1, ref num1);
    233 internal static bool TryParse(string Value, ref double Result)
    234 {
    235       bool flag1;
    236       CultureInfo info1 = Utils.GetCultureInfo();
    237       NumberFormatInfo info3 = info1.NumberFormat;
    238       NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);
    239       Value = StringType.ToHalfwidthNumbers(Value, info1);
    240       if (info3 == info2)
    241       {
    242             return double.TryParse(Value, NumberStyles.Any, info2, out Result);
    243       }
    244       try
    245       {
    246             Result = double.Parse(Value, NumberStyles.Any, info2);
    247             flag1 = true;
    248       }
    249       catch (FormatException)
    250       {
    251             flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);
    252       }
    253       catch (Exception)
    254       {
    255             flag1 = false;
    256       }
    257       return flag1;
    258 }
    View Code
  • 相关阅读:
    惠普 HP 战66 AMD 4700U 烫手 发烫,使用 Ryzen Controller 温控
    双网卡、多网卡指定IP分别访问网卡(windows添加静态路由)
    战66 WIN11 亮度热键 快捷键无效
    编程语言界的丐帮 C#.NET 国密数字信封 民生银行
    开机启动VM WARE 某台虚拟机
    编程语言界的丐帮 C#.NET FRAMEWORK 4.6 EF 连接MYSQL
    C# .NET 云南农信国密签名(SM2)简要解析
    错误 CS1617 Invalid option '7.3' for /langversion; must be ISO1, ISO2, Default or an integer in range 1 to 6.
    windows 命令行调整跃点数
    C#.NET BC BouncyCastle 国密 SM2 解密遇到 Invalid point encoding 77
  • 原文地址:https://www.cnblogs.com/ahdsxhs/p/10919326.html
Copyright © 2020-2023  润新知