C# IsNullOrWhiteSpace() Method


The IsNullOrWhiteSpace() method in C# is used to indicate whether a specified string is null, empty, or consists only of white-space characters.

Syntax

public static bool IsNullOrWhiteSpace (string val);

Above, the parameter val is the string to test. Let us now see an example −

Example

Let us now see an example:

 Live Demo

using System;
public class Demo {
   public static void Main() {
      string str1 = null;
      string str2 = String.Empty;
      Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));
      Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));
   }
}

Output

This will produce the following output -

Is string1 null or whitespace? = True
Is string2 null or whitespace? = True

Example

Let us now see another example -

 Live Demo

using System;
public class Demo {
   public static void Main() {
      string str1 = "
";       string str2 = "Tim";       Console.WriteLine("Is string1 null or whitespace? = "+String.IsNullOrWhiteSpace(str1));       Console.WriteLine("Is string2 null or whitespace? = "+String.IsNullOrWhiteSpace(str2));    } }

Output

Is string1 null or whitespace? = True
Is string2 null or whitespace? = False

Updated on: 03-Dec-2019

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements