AsBoolean
Converts a string to a boolean value if possible or throws an exception
Source
public static bool AsBoolean(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
"y".AsBoolean() //returns true
"NO".AsBoolean() //returns false