Check if a string is blank or doesn't exist in Scala

前端 未结 9 1166
误落风尘
误落风尘 2021-02-01 16:29

I have an Option[String].

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

def isBlank( input : Option[Strin         


        
9条回答
  •  太阳男子
    2021-02-01 16:56

    All proposed solutions will crash with NullPointerException if you pass:

    val str : Option[String] = Some(null). 
    

    Therefore null-check is a must:

    def isBlank(input: Option[String]): Boolean = 
      input.filterNot(s => s == null || s.trim.isEmpty).isEmpty
    

提交回复
热议问题