IfEmpty and ifBrank in Kotlin

If you want to check for whitespace, if else is fine, but ifEmpty and ifBlank are also useful You can use.

If else

val result = if (dummyText.isEmpty()) "hello" else ""
println(result) // hello

IfEmpty makes me feel better.

val result = dummyText.ifEmpty { "hello" }
println(result) // hello

ifBlank can also handle null.

val result: String? = dummyText? " } ? : "null"
println(result) // hello
Share