
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
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