Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 225849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:27:14+00:00 2026-05-11T19:27:14+00:00

I have a string that may have whitespace characters around it and I want

  • 0

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:

1  if (myString.Trim().Length == 0)
2  if (myString.Trim() == "")
3  if (myString.Trim().Equals(""))
4  if (myString.Trim() == String.Empty)
5  if (myString.Trim().Equals(String.Empty))

I’m aware that this would usually be a clear case of premature optimization, but I’m curious and there’s a chance that this will be done enough to have a performance impact.

So which of these is the most efficient method?

Are there any better methods I haven’t thought of?


Edit: Notes for visitors to this question:

  1. There have been some amazingly detailed investigations into this question – particularly from Andy and Jon Skeet.

  2. If you’ve stumbled across the question while searching for something, it’s well worth your while reading at least Andy’s and Jon’s posts in their entirety.

It seems that there are a few very efficient methods and the most efficient depends on the contents of the strings I need to deal with.

If I can’t predict the strings (which I can’t in my case), Jon’s IsEmptyOrWhiteSpace methods seem to be faster generally.

Thanks all for your input. I’m going to select Andy’s answer as the “correct” one simply because he deserves the reputation boost for the effort he put in and Jon has like eleventy-billion reputation already.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:27:14+00:00Added an answer on May 11, 2026 at 7:27 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a user provided string that may contain unicode characters, and we want
I have a string for example, that may contain special characters ( + ,
I have trouble using Perl grep() with a string that may contain chars that
I have string that I want to chop to array of substrings of given
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have an array that contains string which may contain whitespaces appended to the
I am currently working on a project where I have several string that may
I have a string that looks like: www.blah.com/asdf/asdf/asdfasedf/123 The string may have a slash
I have a variable named String that may have values like the following ones:
I have a string which may or may not end with whitespace and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.