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

前端 未结 9 1151
误落风尘
误落风尘 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
    
    0 讨论(0)
  • 2021-02-01 16:58

    An approach based in pattern matching,

    def isBlank( input : Option[String]) : Boolean = 
      input match {
        case None    => true
        case Some(s) => s.trim.isEmpty
      }
    
    0 讨论(0)
  • 2021-02-01 16:58

    This should work as well since filter of an empty Option results in an empty Option

    def isBlank( input : Option[String]) : Boolean =  
       input.filter(_.trim.length > 0).isEmpty 
    
    0 讨论(0)
  • 2021-02-01 16:59

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

    myOption.exists(_.trim.nonEmpty)
    

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

    0 讨论(0)
  • 2021-02-01 17:02

    exists (Accepted solution) will work when input has at least one element in it, that is Some("") but not when it's None.

    exists checks if at least one element(x) applies to function.

    eg.

    scala> List[String]("apple", "").exists(_.isEmpty)
    res21: Boolean = true
    
    //if theres no element then obviously returns false
    scala> List[String]().exists(_.isEmpty)
    res30: Boolean = false
    

    Same happens with Option.empty, as theres no element in it,

    scala> Option.empty[String].exists(_.isEmpty)
    res33: Boolean = false
    

    So forall is what makes sure the the function applies all the elements.

    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
    

    The gross way is to filter nonEmpty string, then check option.isEmpty.

    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
    
    0 讨论(0)
  • 2021-02-01 17:04

    I am from C# background and found Scala implicit methods similar to C# extensions

    import com.foo.bar.utils.MyExtensions._
    ...
    
    "my string".isNullOrEmpty  // false
    "".isNullOrEmpty           // true
    " ".isNullOrEmpty          // true
    "  ".isNullOrEmpty         // true
    
    val str: String  = null
    str.isNullOrEmpty          // true
    

    Implementation

    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)
    }
    
    0 讨论(0)
提交回复
热议问题