How to check if a string is Null or Empty or Whitespace in C# or VB.NET

To check if a string is Null or Empty or Whitespace in C# or VB.NET you can use the following Extension Method.

Sample C#

public static bool IsNotNullOrEmptyOrWhiteSpace(this string input)
{
	return !input.IsNullOrEmptyOrWhiteSpace();
}

public static bool IsNullOrEmptyOrWhiteSpace(this string input)
{
	return string.IsNullOrEmpty(input) || input.Trim() == string.Empty;
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsNotNullOrEmptyOrWhiteSpace(input As String) As Boolean
	Return Not input.IsNullOrEmptyOrWhiteSpace()
End Function

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsNullOrEmptyOrWhiteSpace(input As String) As Boolean
	Return String.IsNullOrEmpty(input) OrElse input.Trim() = String.Empty
End Function

One thought on “How to check if a string is Null or Empty or Whitespace in C# or VB.NET

Leave a Reply