What's the most efficient way to determine whether an untrimmed string is empty in C#?

前端 未结 8 1590

I have a string that may have whitespace characters around it and I want to check to see whether it is essentially empty.

There are quite a few ways to do this:

相关标签:
8条回答
  • 2020-12-16 00:21

    Edit: New tests:

    Test orders:
    x. Test name
    Ticks: xxxxx //Empty String
    Ticks: xxxxx //two space
    Ticks: xxxxx //single letter
    Ticks: xxxxx //single letter with space
    Ticks: xxxxx //long string
    Ticks: xxxxx //long string  with space
    
    1. if (myString.Trim().Length == 0)
    ticks: 4121800
    ticks: 7523992
    ticks: 17655496
    ticks: 29312608
    ticks: 17302880
    ticks: 38160224
    
    2.  if (myString.Trim() == "")
    ticks: 4862312
    ticks: 8436560
    ticks: 21833776
    ticks: 32822200
    ticks: 21655224
    ticks: 42358016
    
    
    3.  if (myString.Trim().Equals(""))
    ticks: 5358744
    ticks: 9336728
    ticks: 18807512
    ticks: 30340392
    ticks: 18598608
    ticks: 39978008
    
    
    4.  if (myString.Trim() == String.Empty)
    ticks: 4848368
    ticks: 8306312
    ticks: 21552736
    ticks: 32081168
    ticks: 21486048
    ticks: 41667608
    
    
    5.  if (myString.Trim().Equals(String.Empty))
    ticks: 5372720
    ticks: 9263696
    ticks: 18677728
    ticks: 29634320
    ticks: 18551904
    ticks: 40183768
    
    
    6.  if (IsEmptyOrWhitespace(myString))  //See John Skeet's Post for algorithm
    ticks: 6597776
    ticks: 9988304
    ticks: 7855664
    ticks: 7826296
    ticks: 7885200
    ticks: 7872776
    
    7. is (string.IsNullOrEmpty(myString.Trim())  //Cloud's suggestion
    ticks: 4302232
    ticks: 10200344
    ticks: 18425416
    ticks: 29490544
    ticks: 17800136
    ticks: 38161368
    

    And the code used:

    public void Main()
    {
    
        string res = string.Empty;
    
        for (int j = 0; j <= 5; j++) {
    
            string myString = "";
    
            switch (j) {
    
                case 0:
                    myString = "";
                    break;
                case 1:
                    myString = "  ";
                    break;
                case 2:
                    myString = "x";
                    break;
                case 3:
                    myString = "x ";
                    break;
                case 4:
                    myString = "this is a long string for testing triming empty things.";
                    break;
                case 5:
                    myString = "this is a long string for testing triming empty things. ";
    
                    break;
            }
    
            bool result = false;
            Stopwatch sw = new Stopwatch();
    
            sw.Start();
            for (int i = 0; i <= 100000; i++) {
    
    
                result = myString.Trim().Length == 0;
            }
            sw.Stop();
    
    
            res += "ticks: " + sw.ElapsedTicks + Environment.NewLine;
        }
    
    
        Console.ReadKey();  //break point here to get the results
    }
    
    0 讨论(0)
  • 2020-12-16 00:23

    String.IsNullOrWhitespace in .NET 4 Beta 2 also plays in this space and doesnt need to be custom written

    0 讨论(0)
提交回复
热议问题