c#你怎麼判斷一個字串是否是數字?

來源:互聯網
上載者:User
今天在寫代碼時突然想起測試經常用Microsoft.VisualBasic.Information.IsNumeric判斷 url參數是否為數字時的這個方法的效率
因為數字是字串是直接使用的,所以不需要轉型,也就沒有用tryparse
結果一測試嚇一跳,這個方法的效率是如此的低,再測試了下tryparse還不錯,正則的也比較差,
沒什麼技術含量,看結果吧:

先拓展下字串:

code

   public static class Common
    {
        //isDigit
        public static bool isNumberic1(this string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            foreach (char c in _string)
            {
                if (!char.IsDigit(c))//if(c<'0' || c>'9')//最好的方法,在下面測試資料中再加一個0,然後這種方法效率會搞10毫秒左右
                    return false;
            }
            return true;
        }

        //vb isnumberic
        public static bool isNumberic2(this string _string)
        {
            return !string.IsNullOrEmpty(_string) && Microsoft.VisualBasic.Information.IsNumeric(_string);
        }
        //try parse
        public static bool isNumberic3(this string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            int i = 0;
            return int.TryParse(_string, out i);
        }

        //try catch
        public static bool isNumberic4(this string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            try { int.Parse(_string); }
            catch { return false; }
            return true;
        }
        //regex
        public static bool isNumberic5(this string _string)
        {
            return !string.IsNullOrEmpty(_string) && Regex.IsMatch(_string, "^\\d+$");
        }
    }

 

測試的代碼:

 

code

class Program
    {
        static void Main(string[] args)
        {

            Test("1234");
            Test("1234a");
            Test("a1234");
            Test("");
            Test(null);
            
        }

        static void Test(string str)
        {
            bool res1 = false, res2 = false, res3 = false, res4 = false, res5 = false;
            Stopwatch wat = new Stopwatch();
            wat.Start();//
            for (int i = 1; i < 100000; i++)
            {
                res1 = str.isNumberic1();
            }
            wat.Stop();
            Console.WriteLine("isDigit      {0}:{1},{2}", str, wat.ElapsedMilliseconds, res1);

            wat.Reset();
            wat.Start();
            for (int i = 1; i < 100000; i++)
            {
                res2= str.isNumberic2();
            }
            wat.Stop();
            Console.WriteLine("isNumberic   {0}:{1},{2}", str, wat.ElapsedMilliseconds, res2);

            wat.Reset();
            wat.Start();
            for (int i = 1; i < 100000; i++)
            {
                res3 = str.isNumberic3();
            }
            wat.Stop();
            Console.WriteLine("try parse    {0}:{1},{2}", str, wat.ElapsedMilliseconds, res3);

            wat.Reset();
            wat.Start();
            for (int i = 1; i < 100000; i++)
            {
                res4 = str.isNumberic4();
            }
            wat.Stop();
            Console.WriteLine("try catch    {0}:{1},{2}", str, wat.ElapsedMilliseconds, res4);

            wat.Reset();
            wat.Start();
            for (int i = 1; i < 100000; i++)
            {
                res5 = str.isNumberic5();
            }
            wat.Stop();
            Console.WriteLine("regex        {0}:{1},{2}", str, wat.ElapsedMilliseconds, res5);
            Console.WriteLine();
        }
    }

 

下面是我原生測試結果

 

isDigit      1234:5,True
isNumberic   1234:166,True
try parse    1234:21,True
try catch    1234:22,True
regex        1234:134,True

isDigit      1234a:5,False
isNumberic   1234a:196,False
try parse    1234a:19,False
try catch    1234a:5182,False
regex        1234a:150,False

isDigit      a1234:2,False
isNumberic   a1234:184,False
try parse    a1234:16,False
try catch    a1234:5084,False
regex        a1234:106,False

isDigit      :1,False
isNumberic   :0,False
try parse    :0,False
try catch    :1,False
regex        :0,False

isDigit      :1,False
isNumberic   :0,False
try parse    :1,False
try catch    :1,False
regex        :0,False

 

結果:迴圈判斷是否是數字字元效率是最高的
而VisualBasic的方法效率比較低了
順便測試了下VisualBasic裡的left和right方法效率也一樣的低,還不及substring的十分之一
所以VisualBasic裡雖然有幾個方法從名字看來比較好用,實際卻比較杯具。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.