IsNullOrWhiteSpace() Method in C#


This method returns true if the entered string has only whitespace characters or is null.

Let say you checked for null value −

bool val1 = string.IsNullOrWhiteSpace(null);

The above returns True, since the string is “null”. In the same way, check it for whitespace character.

Here is the entire example that checks for null and whitespace.

Example

 Live Demo

using System;
using System.IO;
public class Demo {
   public static void Main() {
      bool val1 = string.IsNullOrWhiteSpace(null);
      bool val2 = string.IsNullOrWhiteSpace(" ");
      bool val3 = string.IsNullOrWhiteSpace("5");
      Console.WriteLine(val1);
      Console.WriteLine(val2);
      Console.WriteLine(val3);
   }
}

Output

True
True
False

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements