Home
Search
C# string.IsNullOrEmpty, IsNullOrWhiteSpaceUse the string.IsNullOrEmpty method to test strings. Also call IsNullOrWhiteSpace.
IsNullOrEmpty, IsNullOrWhiteSpace. The string.IsNullOrEmpty method tests strings. It checks for string references that are null or have no data. IsNullOrWhiteSpace tests for whitespace.
C# string info. Strings are reference types and can be equal to null. Often our programs want to handle empty, null, or whitespace strings in special ways. These 2 methods are ideal here.
Empty String
IsNullOrEmpty. Here, we look at examples of IsNullOrEmpty. The 3 parts in the code samples how the IsNullOrEmpty method matching a null string, an empty string, and then a normal string.
Null Strings
String Literal
Important We cannot check the first char of an empty string. An IndexOutOfRangeException will occur.
Part A We test a null string with string.IsNullOrEmpty and the results are as expected—the method returns true.
Part B We test an empty string literal with string.IsNullOrEmpty, and the method returns true again.
Part C The name of the site author does not return true, because it is not null or empty.
C# program that uses string.IsNullOrEmpty
using System; class Program { static void Main() { // Part A: test with IsNullOrEmpty. string s = null; if (string.IsNullOrEmpty(s) == true) { // True. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } // Part B: test with IsNullOrEmpty again. string y = ""; if (string.IsNullOrEmpty(y) == true) { // True. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } // Part C: test with IsNullOrEmpty without writing true. string z = "sam"; if (string.IsNullOrEmpty(z)) { // False. Console.WriteLine("Null or empty"); } else { Console.WriteLine("Not null and not empty"); } } }
Null or empty Null or empty Not null and not empty
IsNullOrWhiteSpace. This method tests an entire string. It returns true if the string contains only whitespace characters or is null.
Null
Note IsNullOrWhiteSpace handles all whitespace (not just spaces). It scans the entire input string if needed.
Tip The string must be entirely whitespace chars or null for the result to be true.
C# program that uses IsNullOrWhiteSpace
using System; class Program { static void Main() { bool a = string.IsNullOrWhiteSpace(" "); bool b = string.IsNullOrWhiteSpace("\n"); bool c = string.IsNullOrWhiteSpace("\r\n\v "); bool d = string.IsNullOrWhiteSpace(null); bool e = string.IsNullOrWhiteSpace(""); bool f = string.IsNullOrWhiteSpace(string.Empty); bool g = string.IsNullOrWhiteSpace("dotnetperls"); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); Console.WriteLine(e); Console.WriteLine(f); Console.WriteLine(g); } }
True True True True True True False
Benchmark, IsNullOrEmpty. We compare ways of checking strings. The string is tested against null before checking its length—this avoids a null reference exception.
Version 1 We call the string.IsNullOrEmpty method on the string, and it returns false each time.
Version 2 We test the string for null, and also to see if it has a Length of 0. So we inline the tests.
Result We find that string.IsNullOrEmpty is slower than inlined tests. But the code for it is much clearer, which is a huge positive.
C# program that benchmarks string.IsNullOrEmpty
using System; using System.Diagnostics; class Program { const int _max = 100000000; static void Main() { string tested = "test"; var s1 = Stopwatch.StartNew(); // Version 1: call IsNullOrEmpty. for (int i = 0; i < _max; i++) { if (string.IsNullOrEmpty(tested)) { return; } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use inlined tests. for (int i = 0; i < _max; i++) { if (tested == null || tested.Length == 0) { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
1.34 ns string.IsNullOrEmpty 0.84 ns null, Length
Notes, IsNullOrEmpty. This method detects null and empty strings in a single call. It is static—this means it can be called to test a null string, and no exception is raised.
Null
Static
NullReferenceException
Return IsNullOrEmpty returns bool—either true or false. This indicates whether the string is empty or null.
So In other words, if the string has something substantial in it, the result bool will be false.
Bool
Internals, IsNullOrEmpty. How is IsNullOrEmpty implemented? It simply tests for null and then a Length equal to 0. It provides no magic optimizations (as testing also shows).
A summary. IsNullOrEmpty, a string method, gives us a way to check whether a string is acceptable to save or use. For performance it may be better to use manual null checks.
Review, methods. The string.IsNullOrWhiteSpace method provides a handy and efficient way to determine some information about a string in your program.
Home
© 2007-2021 sam allen. see site info on the changelog