C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: These methods can handle "true" and "false" and the uppercased versions of these things.
Warning: Invalid strings, such as "Perls", should be used with the bool.TryParse method to avoid exceptions being thrown.
StringsC# program that uses bool.Parse and bool.TryParse
using System;
class Program
{
static void Main()
{
string value = "true";
bool b = bool.Parse(value);
Console.WriteLine(b); // [1]
value = "False";
b = bool.Parse(value);
Console.WriteLine(b); // [2]
value = "Perls";
if (bool.TryParse(value, out b))
{
Console.WriteLine("Not reached");
}
}
}
Output
True
False
Info: In this example, the static BoolParser class has three methods that help identify the equivalent bool value for an input string.
StaticAnd: The first 2 methods, GetValue and IsFalse, call internally to IsTrue. The IsTrue method checks for true values.
Note: IsTrue determines whether the input matches True, Yes, or 1. It is wrapped in a try-catch block.
Finally: This exception handling could be useful if you call this method in your initialization code, where exceptions could be disastrous.
TryExceptionC# program that demonstrates bool parser
using System;
class Program
{
static void Main()
{
Console.WriteLine(BoolParser.GetValue("true")); // True
Console.WriteLine(BoolParser.GetValue("okay")); // False
Console.WriteLine(BoolParser.GetValue("T")); // True
Console.WriteLine(BoolParser.GetValue("False")); // False
Console.WriteLine(BoolParser.GetValue("No")); // False
Console.WriteLine(BoolParser.GetValue("maybe")); // False
Console.WriteLine(BoolParser.GetValue("YES")); // True
Console.WriteLine(BoolParser.GetValue("TRUE ")); // True
Console.WriteLine(BoolParser.GetValue("f")); // False
Console.WriteLine(BoolParser.GetValue("1")); // True
Console.WriteLine(BoolParser.GetValue("0")); // False
Console.WriteLine(BoolParser.GetValue(bool.TrueString)); // True
Console.WriteLine(BoolParser.GetValue(bool.FalseString)); // False
}
}
/// <summary>
/// Parse strings into true or false bools using relaxed parsing rules
/// </summary>
public static class BoolParser
{
/// <summary>
/// Get the boolean value for this string
/// </summary>
public static bool GetValue(string value)
{
return IsTrue(value);
}
/// <summary>
/// Determine whether the string is not True
/// </summary>
public static bool IsFalse(string value)
{
return !IsTrue(value);
}
/// <summary>
/// Determine whether the string is equal to True
/// </summary>
public static bool IsTrue(string value)
{
try
{
// 1
// Avoid exceptions
if (value == null)
{
return false;
}
// 2
// Remove whitespace from string
value = value.Trim();
// 3
// Lowercase the string
value = value.ToLower();
// 4
// Check for word true
if (value == "true")
{
return true;
}
// 5
// Check for letter true
if (value == "t")
{
return true;
}
// 6
// Check for one
if (value == "1")
{
return true;
}
// 7
// Check for word yes
if (value == "yes")
{
return true;
}
// 8
// Check for letter yes
if (value == "y")
{
return true;
}
// 9
// It is false
return false;
}
catch
{
return false;
}
}
}
Output
True
False
True
False
False
False
True
True
False
True
False
True
False
Specification:
String input: true
Bool result: True
String input: false
Bool result: False
String input: t
Bool result: True
String input: f
Bool result: False
String input: yes
Bool result: True
String input: no
Bool result: False
String input: 1
Bool result: True
String input: 0
Bool result: False
String input: Is invalid
Bool result: False
And: With this class, the utility program is less of a burden because it can accept more input styles.
Tip: No one needs to worry about uppercase or lowercase, or even whitespace. Users can be assured their input will be accepted.
Also: There is a bool.Parse method in the base class library for .NET. It has few options.
System.Boolean.Parse: Microsoft Docs