String 检查字符串是否为空或不为';不存在于Scala中
我有一个String 检查字符串是否为空或不为';不存在于Scala中,string,scala,option,String,Scala,Option,我有一个选项[String] 我想检查是否有一个字符串存在,如果它存在,它不是空的 def isBlank( input : Option[String]) : Boolean = { input.isEmpty || input.filter(_.trim.length > 0).isEmpty } 在Scala中有更好的方法吗 您应该使用exists进行检查。像这样: myOption.exists(_.trim.nonEmpty) 当且仅当选项[Str
选项[String]
我想检查是否有一个字符串存在,如果它存在,它不是空的
def isBlank( input : Option[String]) : Boolean =
{
input.isEmpty ||
input.filter(_.trim.length > 0).isEmpty
}
在Scala中有更好的方法吗 您应该使用
exists
进行检查。像这样:
myOption.exists(_.trim.nonEmpty)
当且仅当
选项[String]
不为None
且不为空时,它将返回True
。这也应该可以工作,因为空选项的筛选会导致空选项
def isBlank( input : Option[String]) : Boolean =
input.filter(_.trim.length > 0).isEmpty
一种基于模式匹配的方法
def isBlank( input : Option[String]) : Boolean =
input match {
case None => true
case Some(s) => s.trim.isEmpty
}
如果您通过以下步骤,所有建议的解决方案都将因NullPointerException而崩溃:
val str : Option[String] = Some(null).
因此,空检查是必须的:
def isBlank(input: Option[String]): Boolean =
input.filterNot(s => s == null || s.trim.isEmpty).isEmpty
我来自C#背景,发现了类似于C#扩展的Scala隐式方法
import com.foo.bar.utils.MyExtensions._
...
"my string".isNullOrEmpty // false
"".isNullOrEmpty // true
" ".isNullOrEmpty // true
" ".isNullOrEmpty // true
val str: String = null
str.isNullOrEmpty // true
实施
package com.foo.bar.utils
object MyExtensions {
class StringEx(val input: String) extends AnyVal {
def isNullOrEmpty: Boolean =
if (input == null || input.trim.isEmpty)
true
else
false
}
implicit def isNullOrEmpty(input: String): StringEx = new StringEx(input)
}
存在
(公认的解决方案)将在输入中至少包含一个元素时起作用,即部分(“”
),但在无
时不起作用
存在
检查是否至少有一个元素(x
)应用于函数。
scala> def isEmpty(sOpt: Option[String]) = sOpt.forall(_.trim.isEmpty)
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(Some(""))
res10: Boolean = true
scala> isEmpty(Some("non-empty"))
res11: Boolean = false
scala> isEmpty(Option(null))
res12: Boolean = true
scala> def isEmpty(sOpt: Option[String]) = sOpt.filter(_.trim.nonEmpty).isEmpty
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(None)
res20: Boolean = true
scala> isEmpty(Some(""))
res21: Boolean = true
例如
选项也会发生同样的情况。为空
,因为其中没有元素
scala> Option.empty[String].exists(_.isEmpty)
res33: Boolean = false
因此forall
是确保函数应用所有元素的关键。
scala> def isEmpty(sOpt: Option[String]) = sOpt.forall(_.trim.isEmpty)
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(Some(""))
res10: Boolean = true
scala> isEmpty(Some("non-empty"))
res11: Boolean = false
scala> isEmpty(Option(null))
res12: Boolean = true
scala> def isEmpty(sOpt: Option[String]) = sOpt.filter(_.trim.nonEmpty).isEmpty
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(None)
res20: Boolean = true
scala> isEmpty(Some(""))
res21: Boolean = true
总的方法是过滤非空的
字符串,然后选中选项。isEmpty
scala> def isEmpty(sOpt: Option[String]) = sOpt.forall(_.trim.isEmpty)
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(Some(""))
res10: Boolean = true
scala> isEmpty(Some("non-empty"))
res11: Boolean = false
scala> isEmpty(Option(null))
res12: Boolean = true
scala> def isEmpty(sOpt: Option[String]) = sOpt.filter(_.trim.nonEmpty).isEmpty
isEmpty: (sOpt: Option[String])Boolean
scala> isEmpty(None)
res20: Boolean = true
scala> isEmpty(Some(""))
res21: Boolean = true
我添加了一个Scalafiddle来处理这个问题: 这表明标记的正确答案是错误的(正如prayagupd所指出的): 该解决方案适用于非空白:
def isNotBlank(str: Option[String]): Boolean =
str.exists(_.trim.nonEmpty)
你也可以利用。它使代码更具声明性 例如:
object NonBlank {
def unapply(s: String): Option[String] = Option(s).filter(_.trim.nonEmpty)
}
然后像这样使用它
def createUser(name: String): Either[Error, User] = name match {
case NonBlank(username) => Right(userService.create(username))
case _ => Left(new Error("Invalid username. Blank usernames are not allowed."))
}
您也可以使用
lastpoption
或headOption
如果字符串为空,则返回None
scala> "hello".lastOption
res39: Option[Char] = Some(o)
scala> "".lastOption
res40: Option[Char] = None
这是不正确的:scala>val myOption:Option[String]=None myOption:Option[String]=None scala>myOption.exists(u.trim.nonEmpty)res1:Boolean=false据我所知,他希望在None上为True。我喜欢你的短代码,但恰恰相反。应该是-myOption.forall(u.trim.isEmpty),这同样有效。我在这里避免在示例代码中使用forall,因为None的含义不明显。如果值为null,则失败。如果你有一个
Some(null)
你做的选项
错了,你知道如何设置可能的null吗?@KapilMalhotra。如果您使用选项
构造函数,它将自动转换为None
。相反,如果您将值传递到Some
构造函数中,您将失去安全保证。但您不应该传递Some(null)
。它破坏了使用选项的好处
!这是一种公开的方法,你永远不知道有人会如何违反你提供的合同。因此,我认为,反复检查并且不要踩空指针总是好的。您可以使用隐式类btw:implicit class StringExtensions(val input:String){def isNullOrEmpty:Boolean=input==null | | | | input.trim.isEmpty}不,只需将exists(u.trim.nonEmpty)
更改为forall(u.trim.isEmpty)
.hoho,你真有魔力。我走了一条愚蠢的路线:)谢谢,更新的答案我添加了一个Scalafiddle来处理这个问题:这证明了选中的答案是不正确的。这解决了一个字符串,而不是一个选项[String]
。此外,空字符串“”
”与“空白”字符串(即空白)不同。