C# String - IsNullOrWhiteSpace() Method



The C# String IsNullOrWhiteSpace() method is used to check whether the specified string is null, empty (""), or contains only white-space characters (" ").

A string is considered null if it has been assigned a null value, a string is empty if it is assigned only an empty pair of double quotes ("") or String.Empty(). Additionally, a string contains only white space means a string is filled with whitespace only (" ").

Syntax

Following is the syntax of the C# string IsNullOrWhiteSpace() method −

public static bool IsNullOrWhiteSpace (string? value);

Parameters

This method accepts a string value as a parameter representing the string to be checked.

Return Value

This method returns true if the value parameter is null or Empty, or if value consists exclusively of white-space characters.

Example 1: Check Whether String is Empty

Following is a basic example of the IsNullOrWhiteSpace() method to check whether string is empty or not −

  
using System;
class Program {
   static void Main() {
      string str = "";
      bool res = string.IsNullOrWhiteSpace(str);
      if (res == true) {
         Console.WriteLine("String is empty.");
      }
      else {
         Console.WriteLine("String is neither empty nor null.");
      }
   }
}

Output

Following is the output −

String is empty.

Example 2: Check Whether String is Null

Let's look at another example. Here, we use the IsNullOrWhiteSpace() method to check string is null or not −

using System;
class Program {
   static void Main() {
      string str = null;
      bool res = string.IsNullOrWhiteSpace(str);
      if (res == true) {
         Console.WriteLine("String is null");
      }
      else {
         Console.WriteLine("String is not null.");
      }
   }
}

Output

Following is the output −

String is null

Example 3: Check Whether String Contains White-Space

Here, in this example, we use the IsNullOrWhiteSpace method to check, whether the string contains white-space character or not −

using System;
class Program { 
   static void Main() {
      string str = "  ";
      bool res = string.IsNullOrWhiteSpace(str);
      if (res == true) {
         Console.WriteLine("String contains white-space");
      }
      else {
         Console.WriteLine("String is not null, not empty, or does not contains white-space");
      }
   }
}

Output

Following is the output −

String contains white-space

Example 4: What If String Contains Value?

The example below uses the IsNullOrWhiteSpace() method to check if a string is empty or null or contains white space. If the string is neither null nor empty and does not contain white-space, it will display a false −

using System;
class Program {
   static void Main() {
      string str = "TutorialsPoint";
      bool res = string.IsNullOrWhiteSpace(str);
      if(res == true){
         Console.WriteLine("String is either Empty or Null");
      }
      else{
         Console.WriteLine("The string is neither Empty nor Null and does not contain white-space");
         Console.WriteLine("string value: {0}", str);
      }
   }
}

Output

Following is the output −

The string is neither Empty nor Null and does not contain white-space
string value: TutorialsPoint
csharp_strings.htm
Advertisements