In Java you can write Boolean.valueOf(myString)
. However in Scala, java.lang.Boolean
is hidden by scala.Boolean
which lacks this function. It's easy enough to switch to using the original Java version of a boolean, but that just doesn't seem right.
So what is the one-line, canonical solution in Scala for extracting true
from a string?
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
The easiest way to convert string to boolean is to compare the string with 'true' : let myBool = (myString === 'true');
Ah, I am silly. The answer is myString.toBoolean
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With