Skip to content

Instantly share code, notes, and snippets.

@jamiehowarth0
Created September 14, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehowarth0/5587d7d3731d58b10e0559c6e0efb4e1 to your computer and use it in GitHub Desktop.
Save jamiehowarth0/5587d7d3731d58b10e0559c6e0efb4e1 to your computer and use it in GitHub Desktop.
String to boolean conversion
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