Friday, 20 April 2012

C# Tips : New string.IsNullOrWhiteSpace inbuilt methods in .net 4.0

There are situations in which you need to check that string is null or Empty or only space in string exist or not.
Previously we have string.IsNullOrEmpty Method. This method only check for Null and Empty, it does not check which spaces.
.Net 4.0 Introduce new IsNullOrWhiteSpace method in string class.

Syntax :
  string.IsNullOrWhiteSpace(string strValue);

This method return boolean result.

C# Example :
        string strValue=" ";
        if (string.IsNullOrWhiteSpace(strValue)==true)
        {
            Response.Write("String is Null , Empty or contrains only space.");
        }

Output :
    String is Null , Empty or contrains only space.

2 comments:

  1. This is a good additional method to IsNullOrEmpty, and a good one to know about.

    ReplyDelete