How to set a String in a Option[String]?

Solution 1:

You can also just use Option(myValue) which will convert null to None and non-null to Some.

Solution 2:

You can wrap any object in an Option like this:

val opt = Some("foo")

Solution 3:

You can just wrap your object in Some class

val myField = Some(myValue)

Or if you dont have Anything, pass

None

Its called Option pattern

http://www.codecommit.com/blog/scala/the-option-pattern

Solution 4:

If you want to convert empty string to None more universal solution is: Option(str).filter(_.nonEmpty)