程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#斷定一個String能否為數字類型

C#斷定一個String能否為數字類型

編輯:C#入門知識

C#斷定一個String能否為數字類型。本站提示廣大學習愛好者:(C#斷定一個String能否為數字類型)文章只能為提供參考,不一定能成為您想要的結果。以下是C#斷定一個String能否為數字類型正文


計劃一:Try...Catch(履行效力不高)

private bool IsNumberic(string oText)
{
    try
    {
        int var1=Convert.ToInt32 (oText);
        return true;
    }
    catch
    {
        return false;
    }
}

計劃二:正則表達式(推舉)

a)

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*$");
}

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);
}

計劃三:遍歷

a)

public bool isnumeric(string str)
{
    char[] ch=new char[str.Length];
    ch=str.ToCharArray();
    for(int i=0;i    {
        if(ch[i]<48 || ch[i]>57)
            return false;
    }
    return true;
}

b)

public bool IsInteger(string strIn) {
    bool bolResult=true;
    if(strIn=="") {
        bolResult=false;
    }
    else {
        foreach(char Char in strIn) {
            if(char.IsNumber(Char))
                continue;
            else {
                bolResult=false;
                break;
            }
        }
    }
    return bolResult;
}

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;
}

計劃四:改寫vb的IsNumeric源代碼(履行效力不高)

//主調函數
public static bool IsNumeric(object Expression)
{
      bool flag1;
      IConvertible convertible1 = null;
      if (Expression is IConvertible)
      {
            convertible1 = (IConvertible) Expression;
      }
      if (convertible1 == null)
      {
            if (Expression is char[])
            {
                  Expression = new string((char[]) Expression);
            }
            else
            {
                  return false;
            }
      }
      TypeCode code1 = convertible1.GetTypeCode();
      if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
      {
            return Utils.IsNumericTypeCode(code1);
      }
      string text1 = convertible1.ToString(null);
      try
      {
            long num2;
            if (!StringType.IsHexOrOctValue(text1, ref num2))
            {
                  double num1;
                  return DoubleType.TryParse(text1, ref num1);
            }
            flag1 = true;
      }
      catch (Exception)
      {
            flag1 = false;
      }
      return flag1;
}//子函數
// return Utils.IsNumericTypeCode(code1);
internal static bool IsNumericTypeCode(TypeCode TypCode)
{
      switch (TypCode)
      {
            case TypeCode.Boolean:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            {
                  return true;
            }
            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            {
                  break;
            }
      }
      return false;
}
 

//-----------------
//StringType.IsHexOrOctValue(text1, ref num2))
internal static bool IsHexOrOctValue(string Value, ref long i64Value)
{
      int num1;
      int num2 = Value.Length;
      while (num1 < num2)
      {
            char ch1 = Value[num1];
            if (ch1 == '&')
            {
                  ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
                  string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
                  if (ch1 == 'h')
                  {
                        i64Value = Convert.ToInt64(text1, 0x10);
                  }
                  else if (ch1 == 'o')
                  {
                        i64Value = Convert.ToInt64(text1, 8);
                  }
                  else
                  {
                        throw new FormatException();
                  }
                  return true;
            }
            if ((ch1 != ' ') && (ch1 != '/u3000'))
            {
                  return false;
            }
            num1++;
      }
      return false;
}
//----------------------------------------------------
// DoubleType.TryParse(text1, ref num1);
internal static bool TryParse(string Value, ref double Result)
{
      bool flag1;
      CultureInfo info1 = Utils.GetCultureInfo();
      NumberFormatInfo info3 = info1.NumberFormat;
      NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);
      Value = StringType.ToHalfwidthNumbers(Value, info1);
      if (info3 == info2)
      {
            return double.TryParse(Value, NumberStyles.Any, info2, out Result);
      }
      try
      {
            Result = double.Parse(Value, NumberStyles.Any, info2);
            flag1 = true;
      }
      catch (FormatException)
      {
            flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);
      }
      catch (Exception)
      {
            flag1 = false;
      }
      return flag1;
}

計劃五:直接援用vb運轉庫(履行效力不高)

辦法:起首須要添加Visualbasic.runtime的援用
代碼中Using Microsoft.visualbasic;
法式頂用Information.isnumeric("ddddd");

以上就是C#斷定一個String能否為數字類型的全體內容,推舉年夜家應用正則表達式的辦法,比擬簡略且效力高。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved