C# Tutorial C# Advanced C# References

C# String - IsNullOrWhiteSpace() Method



The C# IsNullOrWhiteSpace() method is used to check the specified string for null, empty or whitespaces. It returns true if the specified string is null, empty or whitespaces, else returns false.

Syntax

public static bool IsNullOrWhiteSpace(string value);

Parameters

value Specify the string which need to be tested.

Return Value

Returns true if the specified string is null, empty or whitespaces, else returns false.

Exception

NA.

Example:

In the example below, IsNullOrWhiteSpace() method is used to check the string called MyStr for null, empty or whitespaces.

using System;

class MyProgram {
  static void Main(string[] args) {
    string MyStr;

    MyStr = "Hello";
    Console.WriteLine(String.IsNullOrWhiteSpace(MyStr)); 
    MyStr = "   ";
    Console.WriteLine(String.IsNullOrWhiteSpace(MyStr)); 
    MyStr = "";
    Console.WriteLine(String.IsNullOrWhiteSpace(MyStr));     
    MyStr = null;
    Console.WriteLine(String.IsNullOrWhiteSpace(MyStr)); 
  }
}

The output of the above code will be:

False
True
True
True

❮ C# String Methods