检查字符串是否为空或在Scala中不存在 [英] Check if a string is blank or doesn't exist in Scala

查看:1479
本文介绍了检查字符串是否为空或在Scala中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Option[String].

我想检查是否存在一个字符串,如果存在则不为空.

I want to check if there is a string exists and if it's exists its not blank.

def isBlank( input : Option[String]) : Boolean = 
{ 
     input.isEmpty || 
     input.filter(_.trim.length > 0).isEmpty 
}

在Scala中有更好的方法吗?

Is there is a better way of doing this in Scala ?

推荐答案

您应该做的是使用exists进行检查.像这样:

What you should do is check using exists. Like so:

myOption.exists(_.trim.nonEmpty)

仅当Option[String]不是None并且不为空时,才会返回True.

which will return True if and only if the Option[String] is not None and not empty.

这篇关于检查字符串是否为空或在Scala中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆