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

前端 未结 9 1171
误落风尘
误落风尘 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条回答
  •  猫巷女王i
    2021-02-01 17:06

    I added a Scalafiddle to play with that: Scalafiddle

    That shows the marked correct answer is wrong (as pointed out by prayagupd):

    def isBlank(str: Option[String]): Boolean =
       str.forall(_.trim.isEmpty)
    

    the solution is for non-blank:

    def isNotBlank(str: Option[String]): Boolean =
       str.exists(_.trim.nonEmpty)   
    

提交回复
热议问题