ExtensionMethod.NET Home of 877 C#, Visual Basic, F# and Javascript extension methods

IsEmptyOrWhiteSpace

This method will check if a string is empty or consists only of whitespace characters, but not null or containing any other characters. This check is sometimes required in data validation processes.

Source

//This method will check if a string is empty or consists only of whitespace characters, but not null or containing any other characters. This check is sometimes required in data validation processes.

public static bool IsEmptyOrWhiteSpace(this string value)
{
    return value != null && value.Trim() == string.Empty;
}

Example

Assert.IsFalse(IsEmptyOrWhiteSpace(null));
Assert.IsTrue(IsEmptyOrWhiteSpace(string.Empty));
Assert.IsTrue(IsEmptyOrWhiteSpace("   "));
Assert.IsFalse(IsEmptyOrWhiteSpace("TestValue"));
Assert.IsTrue(IsEmptyOrWhiteSpace(""));

Author: Muhammad Shoaib Ijaz

Submitted on: 16 apr. 2024

Language: csharp

Type: System.String

Views: 373