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 ?
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.
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.
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.
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.
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.
An approach based in pattern matching,
def isBlank( input : Option[String]) : Boolean =
input match {
case None => true
case Some(s) => s.trim.isEmpty
}
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With