Created
September 14, 2021 00:17
-
-
Save jamiehowarth0/5587d7d3731d58b10e0559c6e0efb4e1 to your computer and use it in GitHub Desktop.
String to boolean conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExtensions { | |
public static bool ToBoolean(this string str, bool bDefault = false) { | |
var BooleanStringOff = new[] { "0", "off", "no" }; | |
if (string.IsNullOrEmpty(str)) return bDefault; | |
else if (BooleanStringOff.Contains(str, StringComparer.InvariantCultureIgnoreCase)) return false; | |
bool result; | |
if (!bool.TryParse(str, out result)) result = true; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment