C#判断一串字符串是否为数字字符串

简介:

    当现在有一个字符串,需要判断这个字符串是否可以转为Number类型时,可以运用下面这段代码进行判别。其中包括判别带小数点的数字字符串。

 public bool IsNumeric(string str)
    {
        if (str == null || str.Length == 0)
            return false;
        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] bytestr = ascii.GetBytes(str);

        foreach (byte c in bytestr)
        {
            if (c!=46)//小数点的ASCII码为46
            {
                if (c < 48 || c > 57)//非0~9的ASCII码
                {
                    return false;
                }
            }
            else
            {
                return true;
            }  
        }
        return true;
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2021-12-04
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
相关资源
相似解决方案