KotlinのifEmptyとifBrank

空白チェックを行う場合、if elseでもいいが、ifEmptyやifBlankでも便利にできる。


if elseの場合

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


ifEmpty だとスッキリする

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


ifBlank はnullも扱える

val result: String? = dummyText?.ifBlank { "hello" } ?: "null"
println(result) // hello