There are more concise ways. One of:
Option(x).filter(_ != "") Option(x).filterNot(_ == "")
will do the trick, although it is slightly less efficient, as it creates an option and then can throw it away.
If you do this a lot, you probably want to create an extension method (or just a method if you don't mind the method name first):
implicit class ToOptionWithDefault[A](private val underlying: A) extends AnyVal { def optNot(not: A) = if (underlying == not) None else Some(underlying) }
Now you can
scala> 47.toString optNot "" res1: Option[String] = Some(47)
(And, of course, you can always create a method whose body is your matching solution, or equivalent with if, so you can reuse it for this particular case.)
source share