Scala:将字符串优雅地转换为布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1470230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala: Elegant conversion of a string into a boolean
提问by David Crawshaw
In Java you can write Boolean.valueOf(myString). However in Scala, java.lang.Booleanis hidden by scala.Booleanwhich 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.
在 Java 中,您可以编写Boolean.valueOf(myString). 但是在 Scala 中,java.lang.Boolean隐藏在scala.Boolean其中缺少此功能。切换到使用布尔值的原始 Java 版本很容易,但这似乎不对。
So what is the one-line, canonical solution in Scala for extracting truefrom a string?
那么 Scala 中用于true从字符串中提取的单行规范解决方案是什么?
回答by David Crawshaw
Ah, I am silly. The answer is myString.toBoolean.
啊,我傻了。答案是myString.toBoolean。
回答by user24601
How about this:
这个怎么样:
import scala.util.Try
Try(myString.toBoolean).getOrElse(false)
If the input string does not convert to a valid Boolean value falseis returned as opposed to throwing an exception. This behavior more closely resembles the Java behavior of Boolean.valueOf(myString).
如果输入字符串未转换为有效的布尔值,false则返回与抛出异常相反。这种行为更类似于Boolean.valueOf(myString).
回答by Xavier Guihot
Scala 2.13introduced String::toBooleanOption, which combined to Option::getOrElse, provides a safe way to extract a Booleanas 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
回答by Jesper
Note: Don't write new Boolean(myString)in Java - always use Boolean.valueOf(myString). Using the newvariant unnecessarily creates a Booleanobject; using the valueOfvariant doesn't do this.
注意:不要new Boolean(myString)用 Java编写- 始终使用Boolean.valueOf(myString). new不必要地使用变体会创建一个Boolean对象;使用valueOf变体不会这样做。
回答by Kristian Domagala
The problem with myString.toBooleanis that it will throw an exception if myString.toLowerCaseisn'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"or之一,它将抛出异常"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). Or write your own method that handles your own particular circumstances (eg, you may want "t"to be trueas well).
如果您想要与 完全相同的行为java.lang.Boolean.valueOf,请使用完全限定的,或以不同的名称导入 Boolean,例如,import java.lang.{Boolean=>JBoolean}; JBoolean.valueOf(myString). 或者编写您自己的方法来处理您自己的特定情况(例如,您可能也希望"t"如此true)。
回答by TobyEvans
I've been having fun with this today, mapping a 1/0 set of values to boolean. I had to revert back to Spark 1.4.1, and I finally got it working with:
我今天玩得很开心,将一组 1/0 的值映射到布尔值。我不得不恢复到 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 ...
我以前的版本没有“尝试”,这有效,其他方法也可用......

