Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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[String]) : Boolean = 
{ 
     input.isEmpty || 
     input.filter(_.trim.length > 0).isEmpty 
}

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

like image 226
Soumya Simanta Avatar asked Jun 06 '14 16:06

Soumya Simanta


People also ask

How do you check whether a string is empty or not in Scala?

The main() function is the entry point for the program. In the main() function, we created two immutable strings str1, str2 using StringBuilder class. Then we checked string is empty or not using the isEmpty() method then printed the appropriate message on the console screen.

Is not empty in Scala?

Scala Iterator nonEmpty() method with exampleThe nonEmpty method belongs to the concrete value member of the class Abstract Iterator. It is utilized to check whether the stated collection is not empty. Return Type: It returns true if the stated collection contains at least one element else returns false.

Is empty string null in Scala?

If you're looking to check whether a String is null or empty, here's a one-liner that does that. You wrap it with an Option, use filter to make sure it's not an empty String then call getOrElse to provide a default value in case the original value is either null or an empty String.

Is null in Scala?

null is the value of a reference that is not referring to any object. It can be used as a replacement for all reference types — that is, all types that extend scala. AnyRef. We must try to avoid using null while initializing variables if there's an empty value of the variable's type available, such as Nil.


4 Answers

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.

like image 155
wheaties Avatar answered Oct 16 '22 11:10

wheaties


An approach based in pattern matching,

def isBlank( input : Option[String]) : Boolean = 
  input match {
    case None    => true
    case Some(s) => s.trim.isEmpty
  }
like image 27
elm Avatar answered Oct 16 '22 13:10

elm


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
like image 5
prayagupa Avatar answered Oct 16 '22 13:10

prayagupa


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 
like image 3
Jens Schauder Avatar answered Oct 16 '22 13:10

Jens Schauder