Scala: Elegant conversion of a string into a boolean

convert string to boolean
scala boolean to string
scala convert int to boolean
scala boolean to 0 or 1
scala if-else alternative
scala boolean to java boolean
scala option to boolean
scala boolean toint

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?

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

Scala: Elegant conversion of a string into a boolean - scala - html, How about this: import scala.util.Try Try(myString.toBoolean).getOrElse(false) If the input string does not convert to a valid Boolean value false is returned as� जावा में आप Boolean.valueOf (myString) लिख सकते हैं। हालांकि स्कला में, java.lang.Boolean scala.Boolean द्वारा छुपा हुआ है, जो इस

How about this:

import scala.util.Try

Try(myString.toBoolean).getOrElse(false)

If the input string does not convert to a valid Boolean value false is returned as opposed to throwing an exception. This behavior more closely resembles the Java behavior of Boolean.valueOf(myString).

How to convert string to boolean in Scala?, It has two valid values i.e. true and false. Example: var bool : Boolean = true;. Convert string to boolean. The conversion from string to boolean� scala.Boolean abstract final class Boolean extends AnyVal. Boolean (equivalent to Java’s boolean primitive type) is a subtype of scala.AnyVal. Instances of Boolean are not represented by an object in the underlying runtime system. There is an implicit conversion from scala.Boolean => scala.runtime.RichBoolean which provides useful non

Scala 2.13 introduced String::toBooleanOption, which combined to Option::getOrElse, provides a safe way to extract a Boolean as a String:

"true".toBooleanOption.getOrElse(false)  // true
"false".toBooleanOption.getOrElse(false) // false
"oups".toBooleanOption.getOrElse(false)  // false

Convert String to boolean primitive in Java: parseBoolean() method, Description How to convert String object to boolean primitive in Java. Example: Complete conversion code In this example we are converting string to. Boolean.getBoolean() is a third method that accepts a String and returns a boolean. Without looking at the documentation or the implementation of this method, one might reasonably assume that this method is also for converting its String argument into a boolean:

Note: Don't write new Boolean(myString) in Java - always use Boolean.valueOf(myString). Using the new variant unnecessarily creates a Boolean object; using the valueOf variant doesn't do this.

3. Expressions and Conditionals - Learning Scala [Book], Let's start out with an example of a simple expression in Scala, just a String If the Boolean expression returns false, what do you expect the if block to return? There are other, more elegant ways to do so in Scala, however, using match expressions. If you have multiple expressions in a case block, convert them to an� Here's my attempt at the most forgiving string to bool conversion that is still useful, basically keying off only the first character. public static class StringHelpers { /// <summary> /// Convert string to boolean, in a forgiving way.

The problem with myString.toBoolean is that it will throw an exception if myString.toLowerCase isn't exactly one of "true" or "false" (even extra white space in the string will cause an exception to be thrown).

If you want exactly the same behaviour as java.lang.Boolean.valueOf, then use it fully qualified, or import Boolean under a different name, eg, import java.lang.{Boolean=>JBoolean}; JBoolean.valueOf(myString). Or write your own method that handles your own particular circumstances (eg, you may want "t" to be true as well).

Determine if a string is numeric, Create a boolean function which takes in a string and tells whether it is a numeric syntax the language uses for numeric literals or numbers converted from strings. Ruby; 114 Run BASIC; 115 Rust; 116 Scala; 117 Scheme; 118 Seed7 Alternative 2 : use a regular expression (a more elegant solution). No in your case you concatanate a String with a Boolean so there is no way that the compiler figure out that you want an Int instead of the Boolean. You can cast the value to an Int : "insert into blah (boolVal) values (" + (b:Int) + ")" – Patrick Apr 14 '10 at 17:21

MVEL, Property expressions; Boolean expressions; Method invocations; Variable MVEL is an expression language based on Java-syntax, with some marked MVEL allows you to express Lists, Maps and Arrays using simple elegant syntax. Consider This will not fail in MVEL, instead MVEL will convert the array to a String[] . Java Convert String to boolean. We can convert String to boolean in java using Boolean.parseBoolean(string) method. To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns instance of Boolean class. To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE" will return

Determine If All Elements Are the Same in a Java List, Learn different ways to determine if all elements in a list are equal. public boolean verifyAllEqualUsingALoop(List<String> list) { If we convert a List to a HashSet and the resulting size is less than or equal to 1, then we� We can convert a String to a primitive boolean using the Boolean.parseBoolean method or to a wrapped Boolean class using the Boolean.valueOf. Info: By default the converted String will only convert "true" and "false" to the correct representation of a Boolean.

[PDF] Scala Tutorial, patterns in a concise, elegant, and type-safe way. Scala Programming is based on Java, so if you are aware of Java syntax, then it's pretty easy to Convert to String: . Range from U+0000 to U+FFFF. String. A sequence of Chars. Boolean. In the above example, we have used the valueOf() method of String class to convert the boolean variables into strings. Example 2: Convert boolean to String using toString() We can also convert the boolean variables into strings using the toString() method of the Boolean class.

Comments
  • Why not simply use a regular expression?
  • It doesn't sound simple at all.
  • If myString is null your code will throw an Exception, I prefer the next answer approach Try(myString.toBoolean).getOrElse(false)
  • Try(myString.toBoolean).getOrElse(false) will go through the work of throwing and catching the exception. Option(myString).exists(_.toBoolean) avoids that extra effort.
  • Using Try(myString.toBoolean).getOrElse(false) is good. Handles non-matching string cleanly.
  • I think an exception is what you want. What if the user enters "1" instead of "true" or something else? This approach will leave such an error undetected. But now I know about the Try function, which is nice.
  • This is not true (anymore). toLowerCase is done by toBoolean`
  • You don't need the if with the true else false because your conditional returns true or false. All you need is Try(condition).getOrElse(false).