Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Elegant conversion of a string into a boolean

Tags:

scala

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?

like image 937
David Crawshaw Avatar asked Sep 24 '09 07:09

David Crawshaw


People also ask

How do I convert string to boolean?

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".

How do you change a string from boolean to true?

The easiest way to convert string to boolean is to compare the string with 'true' : let myBool = (myString === 'true');


1 Answers

Ah, I am silly. The answer is myString.toBoolean.

like image 98
David Crawshaw Avatar answered Oct 14 '22 04:10

David Crawshaw