Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most optimal method of checking if a string is empty?

To check whether a string is empty I use

var test = string.Empty; 
if (test.Length == 0) Console.WriteLine("String is empty!");
if (!(test.Any())) Console.WriteLine("String is empty!");
if (test.Count() == 0) Console.WriteLine("String is empty!");
if (String.IsNullOrWhiteSpace(test)) Console.WriteLine("String is empty!");
  1. All the above statements produces the same output. What is the optimal method that I should use?

    var s = Convert.ToString(test);    
    s = test.ToString(CultureInfo.InvariantCulture);
    
  2. Again, both statements does the same thing. What is the best method to use?

I tried debugging and how to benchmark the performance of a C# statement?

like image 379
now he who must not be named. Avatar asked Nov 07 '12 09:11

now he who must not be named.


People also ask

How do you test if a string string is empty?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

How do you check if a value is an empty string?

Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty.

Does isEmpty check for empty string?

isEmpty(< string >)​Checks if the <string> value is an empty string containing no characters or whitespace. Returns true if the string is null or empty.

Is null better than empty string?

So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.


2 Answers

First of all the 4 statemens are not giving the same output on all inputs. Try null and the first 3 will throw an exception. And try whithspaces the last one will give you a failty result. So you really have to think about what you want. The best way to go are normally the:

string.IsNullOrEmpty
string.IsNullOrWhiteSpace

Only if you are doing this a few million times you should have a look on how to optimize your code further.

Here some test result, but this can differ on any .net version:

Test results for 100 million iterations:

Equality operator ==:   796 ms 
string.Equals:          811 ms 
string.IsNullOrEmpty:   312 ms 
Length:                 140 ms  [fastest]
Instance Equals:       1077 ms 

source

like image 73
Peter Avatar answered Oct 22 '22 16:10

Peter


I would go for String.IsNullOrWhiteSpace or String.IsNullOrEmpty.

Length, Count and Any could fail if test is null with object null reference]

Update

Unless you are sure your string won't be null, then you will have to check if string is null before testing the length or count or calling any method on the variable.

In these scenario, .Length is not the same as String.IsNullOrWhiteSpace

string test = "    ";
test.Length == 0;  //false
String.IsNullOrWhiteSpace(test); //true
like image 24
codingbiz Avatar answered Oct 22 '22 17:10

codingbiz