The IsNullOrWhiteSpace()
method is a string method in C# that allows you to check if a string is null, empty, or consists only of whitespace characters. It returns a boolean value indicating whether the string meets any of these criteria.
Here’s the syntax for using IsNullOrWhiteSpace()
:
string str = "example string"; bool result = string.IsNullOrWhiteSpace(str);
In the above code, the str
variable contains the string you want to check. By calling string.IsNullOrWhiteSpace(str)
, you determine if the string is null, empty, or consists only of whitespace characters.
The IsNullOrWhiteSpace()
method returns true
if the string is null, empty, or whitespace-only, and false
otherwise. You can use the result to perform conditional logic or validation based on the string’s content.
Here’s an example:
string userInput = Console.ReadLine(); if (string.IsNullOrWhiteSpace(userInput)) { Console.WriteLine("The input is null, empty, or whitespace-only."); } else { Console.WriteLine("The input is not null, empty, or whitespace-only."); }
In the above code, the user is prompted to enter some input. The IsNullOrWhiteSpace()
method is then used to check if the input is null, empty, or whitespace-only. The appropriate message is displayed based on the result of the check.
Note that IsNullOrWhiteSpace()
is available in the System
namespace, so you need to import it or reference it using the fully qualified name System.String.IsNullOrWhiteSpace()
.
Parameter:
The IsNullOrWhiteSpace()
method in C# does not take any parameters. It is invoked directly on a string object or a string literal. Here’s the syntax:
bool result = string.IsNullOrWhiteSpace(str);
In the above code, str
is the string on which you want to perform the check. You pass the string as an argument to the IsNullOrWhiteSpace()
method.
The method will return true
if the string is null, empty, or consists only of whitespace characters. Otherwise, it will return false
.
It’s important to note that the IsNullOrWhiteSpace()
method is a static method of the System.String
class, so it is called directly on the class itself.
Return:
The IsNullOrWhiteSpace()
method in C# returns a boolean value indicating whether the provided string is null, empty, or consists only of whitespace characters.
- If the string is null, empty, or contains only whitespace characters, the method returns
true
. - If the string has any non-whitespace characters, the method returns
false
.
Here’s an example:
string str1 = null; string str2 = ""; string str3 = " "; string str4 = "example"; bool result1 = string.IsNullOrWhiteSpace(str1); // true bool result2 = string.IsNullOrWhiteSpace(str2); // true bool result3 = string.IsNullOrWhiteSpace(str3); // true bool result4 = string.IsNullOrWhiteSpace(str4); // false
In the above code, result1
is true
because str1
is null. result2
is true
because str2
is an empty string. result3
is true
because str3
consists only of whitespace characters. result4
is false
because str4
contains non-whitespace characters.
You can use the returned boolean value to perform conditional logic or validation based on the string’s content.
C# String IsNullOrWhiteSpace() Method Example:
Certainly! Here’s an example that demonstrates the usage of the IsNullOrWhiteSpace()
method in C#:
using System; class Program { static void Main() { Console.WriteLine("Enter a string:"); string userInput = Console.ReadLine(); if (string.IsNullOrWhiteSpace(userInput)) { Console.WriteLine("The input is null, empty, or whitespace-only."); } else { Console.WriteLine("The input is not null, empty, or whitespace-only."); } } }
In this example, the program prompts the user to enter a string. The ReadLine()
method reads the input from the console and stores it in the userInput
variable.
The IsNullOrWhiteSpace()
method is then used to check whether the input is null, empty, or consists only of whitespace characters. If the condition is true, it prints a message indicating that the input is null, empty, or whitespace-only. Otherwise, it prints a message indicating that the input is not null, empty, or whitespace-only.
Here’s how the program execution might look:
Enter a string: Hello, World! The input is not null, empty, or whitespace-only.
In the above example, the input string ” Hello, World!” contains non-whitespace characters, so the condition string.IsNullOrWhiteSpace(userInput)
evaluates to false
, and the program outputs that the input is not null, empty, or whitespace-only.
If the user were to provide an empty string or a string consisting only of whitespace characters, the program would output that the input is null, empty, or whitespace-only.