Check String for Whitespace Characters or Null in C#



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

Let’s say you are checking for whitespace character.

bool val1 = string.IsNullOrWhiteSpace(“ “);

The above returns True, since the string is a whitespace character. In the same way, check it for null using the IsNullOrWhiteSpace() method.

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

Example

using System;
using System.IO;
public class Demo {
   public static void Main() {
      bool val1 = string.IsNullOrWhiteSpace(“100”);
      bool val2 = string.IsNullOrWhiteSpace(" ");
      bool val3 = string.IsNullOrWhiteSpace(null);
      Console.WriteLine(val1);
      Console.WriteLine(val2);
      Console.WriteLine(val3);
   }
}
Updated on: 2020-06-23T07:20:16+05:30

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements