IsBoolean()

public static bool IsBoolean(this string value) {
     var val = value.ToLower().Trim();
     if (val == "false")
          return true;
     if (val == "f")
          return true;
     if (val == "true")
          return true;
     if (val == "t")
          return true;
     if (val == "yes")
          return true;
     if (val == "no")
          return true;
     if (val == "y")
          return true;
     if (val == "n")
          return true;
     
     return false;
}public static bool IsBoolean(this string value) {
     var val = value.ToLower().Trim();
     if (val == "false")
          return false;
     if (val == "f")
          return false;
     if (val == "true")
          return true;
     if (val == "t")
          return true;
     if (val == "yes")
          return true;
     if (val == "no")
          return false;
     if (val == "y")
          return true;
     if (val == "n")
          return false;
     throw new ArgumentException("Value is not a boolean value.");
}
Example:
"yes".IsBoolean() //returns true
"n".IsBoolean() //returns true

Description

Tells whether a value can be coalesced into a boolean

Details

  • Author: Anonymous
  • Submitted on: 12/25/2014 9:08:34 PM
  • Language: C#
  • Type: String
  • Views: 2079

Double click on the code to select all.

 

;