Scala 是否有一个库方法来构建考虑空字符串的 Option-s?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8534172/
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
Does Scala have a library method to build Option-s that takes into account empty strings?
提问by Manuel Bernhardt
I want to filter out empty strings to put them into an Option. So I quickly built this now:
我想过滤掉空字符串以将它们放入一个选项中。所以我现在快速构建了这个:
def StrictOption(s: String) = s match {
case s if s != null && s.trim.length() > 0 => Some(s)
case _ => None
}
Question: is this maybe already somewhere in the standard library?
问题:这可能已经在标准库中的某个地方了吗?
回答by Ben James
I don't think there's one single methodin the standard library to do this, but you can do this much more tersely than your implementation.
我认为标准库中没有一种方法可以做到这一点,但是您可以比您的实现更简洁地做到这一点。
Option(s).filter(_.trim.nonEmpty)
回答by Luigi Plinge
If you care at all about performance then
如果您完全关心性能,那么
if (s.trim.isEmpty) None else Some(s)
is only 4 characters longer than Ben James's solution, and runs 3 times faster, in my benchmark (47 vs 141).
仅比 Ben James 的解决方案长 4 个字符,运行速度快 3 倍,在我的基准测试中(47 对 141)。
回答by Rex Kerr
There's nothing built in; Ben's filter is the best brief version if performance isn't an issue (e.g. you're validating user input). Typically, performance will not be an issue.
没有内置任何东西;如果性能不是问题(例如,您正在验证用户输入),那么 Ben 的过滤器是最好的简短版本。通常,性能不会成为问题。
Also, note that it's a little strange to use matchwhen you're not actually matching anything; it's just more boilerplate to get an if-else statement. Just say
另外,请注意,match当您实际上没有匹配任何内容时使用它有点奇怪;获得if-else语句只是更多样板。说啊
if (s ne null && s.trim.length > 0) Some(s) else None
which is about as fast and brief as anything, unless you want to write your own is-it-whitespace method. Note that trimuses a peculiar criterion: anything above \u0020 (i.e. ' ') is not trimmed, and anything equal or below is. So you could also write your own trimmed-string-is-empty detector, if performance of this operation was particularly important:
除非您想编写自己的 is-it-whitespace 方法,否则这与任何事情一样快速和简短。请注意,trim使用了一个特殊的标准:任何高于 \u0020(即 ' ')的都不会被修剪,而任何等于或低于的都是。因此,如果此操作的性能特别重要,您也可以编写自己的修剪字符串为空检测器:
def ContentOption(s: String): Option[String] = {
if (s ne null) {
var i = s.length-1
while (i >= 0) {
if (s.charAt(i) > ' ') return Some(s)
i -= 1
}
}
None
}
回答by Xavier Guihot
Starting Scala 2.13, for those not expecting nulls (non-Java context), Option::unlessand Option::whenare now an alternative option:
开始Scala 2.13,对于那些没有料到nullS(非Java上下文),Option::unless和Option::when现在是另一种选择:
// val str = "hello"
Option.unless(str.isEmpty)(str)
// Option[String] = Some(hello)
Option.when(str.nonEmpty)(str)
// Option[String] = Some(hello)
// val str: String = ""
Option.unless(str.isEmpty)(str)
// Option[String] = None
Option.when(str.nonEmpty)(str)
// Option[String] = None
回答by darrenmc
This could also be achieved with a for-comprehension
这也可以通过理解来实现
val res = for (v <- Option(s) if s.nonEmpty) yield v
回答by Christopher Hunt
Option("something") produces Some("something")
Option("something") 产生 Some("something")
Option(null) produces None
选项(空)产生无

