The methods you're calling on the Boolean
class don't check whether the string contains a valid boolean value, but they return the boolean value that represents the contents of the string: put "true" in string, they return true
, put "false" in string, they return false
.
You can surely use these methods, however, to check for valid boolean values, as I'd expect them to throw an exception if the string contains "hello" or something not boolean.
Wrap that in a Method ContainsBoolString
and you're go.
EDIT
By the way, in C# there are methods like bool Int32.TryParse(string x, out int i)
that perform the check whether the content can be parsed and then return the parsed result.
int i;
if (Int32.TryParse("Hello", out i))
// Hello is an int and its value is in i
else
// Hello is not an int
Benchmarks indicate they are way faster than the following:
int i;
try
{
i = Int32.Parse("Hello");
// Hello is an int and its value is in i
}
catch
{
// Hello is not an int
}
Maybe there are similar methods in Java? It's been a while since I've used Java...