How to convert String
object to Boolean
object?
public static boolean stringToBool(String s) {
s = s.toLowerCase();
Set<String> trueSet = new HashSet<String>(Arrays.asList("1", "true", "yes"));
Set<String> falseSet = new HashSet<String>(Arrays.asList("0", "false", "no"));
if (trueSet.contains(s))
return true;
if (falseSet.contains(s))
return false;
throw new IllegalArgumentException(s + " is not a boolean.");
}
My way to convert string to boolean.
This is how I did it:
"1##true".contains( string )
For my case is mostly either 1 or true. I use hashes as dividers.