天天看點

C#判斷字元串是否為純數字

//判斷字元串是否為純數字
        public static bool IsNumber(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
        }