Could anyone please explain how can I check if String is null or empty?
I have a below code which is giving different result explain why.
val someMap = ListMap[String,String]("key1" -> "")
val s = ""
println("s.isEmpty() : "+s.isEmpty())
println("someMap.get(\"key1\") : "+someMap.get("key1").toString().isEmpty)
Result is
s.isEmpty() : true
someMap.get("key1") : false
But why?
This is because
Map.getreturns an Option: eitherSome(value)if the value is in the Map orNone, if there is no such key in Map.If you turn
Some("")to a string you’ll get"Some()"which is definitely not empty.To achieve the behavior you wanted, write your code as