C# judges whether a string is a pure number

c bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length == 0)    //验证这个参数是否为空
                return false;                           //是,就返回False
             ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
             byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里
 
             foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是,就返回False
                }
            }
            return true;                                        //是,就返回True
        }
 
//备注 数字,字母的ASCII码对照表
/*
0~9数字对应十进制48-57 
a~z字母对应的十进制97-122十六进制61-7A 
A~Z字母对应的十进制65-90十六进制41-5A
*/

Reprint: Judging whether a string is a pure number in C#_weixin_33980459's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130886851