简体   繁体   English

Scala:将字符串优雅地转换为布尔值

[英]Scala: Elegant conversion of a string into a boolean

In Java you can write Boolean.valueOf(myString) . 在Java中,您可以编写Boolean.valueOf(myString) However in Scala, java.lang.Boolean is hidden by scala.Boolean which lacks this function. 但是在Scala中,缺少此功能的scala.Booleanjava.lang.Boolean隐藏。 It's easy enough to switch to using the original Java version of a boolean, but that just doesn't seem right. 切换到使用原始Java版本的布尔值很容易,但这似乎并不正确。

So what is the one-line, canonical solution in Scala for extracting true from a string? 那么,Scala中用于从字符串中提取true的单行规范解决方案是什么?

Ah, I am silly. 啊,我很傻。 The answer is myString.toBoolean . 答案是myString.toBoolean

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. 如果输入字符串未转换为有效的布尔值,则返回false ,而不是引发异常。 This behavior more closely resembles the Java behavior of Boolean.valueOf(myString) . 此行为更类似于Boolean.valueOf(myString)的Java行为。

Note: Don't write new Boolean(myString) in Java - always use Boolean.valueOf(myString) . 注意:请勿在Java中编写new Boolean(myString) -始终使用Boolean.valueOf(myString) Using the new variant unnecessarily creates a Boolean object; 使用new变量会不必要地创建Boolean对象; using the valueOf variant doesn't do this. 使用valueOf变体不会这样做。

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). myString.toBoolean的问题在于,如果myString.toLowerCase不完全是"true""false"之一,它将引发异常(即使字符串中多余的空格也会引发异常)。

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) 如果您想要与java.lang.Boolean.valueOf完全相同的行为,请使用完全限定的行为,或以其他名称导入Boolean,例如, import java.lang.{Boolean=>JBoolean}; JBoolean.valueOf(myString) import java.lang.{Boolean=>JBoolean}; JBoolean.valueOf(myString) . 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). 或者编写自己的方法来处理自己的特定情况(例如,您可能也希望"t"true )。

Scala 2.13 introduced String::toBooleanOption , which combined to Option::getOrElse , provides a safe way to extract a Boolean as a String : Scala 2.13引入了String::toBooleanOption ,它与Option::getOrElse组合在一起,提供了一种将Boolean提取为String的安全方法:

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

I've been having fun with this today, mapping a 1/0 set of values to boolean. 我今天很开心,将1/0的一组值映射到布尔值。 I had to revert back to Spark 1.4.1, and I finally got it working with: 我不得不恢复到Spark 1.4.1,终于可以使用它了:

Try(if (p(11).toString == "1" || p(11).toString == "true") true else false).getOrElse(false)) 

where p(11) is the dataframe field 其中p(11)是数据框字段

my previous version didn't have the "Try", this works, other ways of doing it are available ... 我以前的版本没有“尝试”功能,该功能可以使用,但其他方法也可以使用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM