inline fun <C, R> C.ifEmpty( defaultValue: () -> R ): R where C : CharSequence, C : R
Returns this char sequence if it's not empty or the result of calling defaultValue function if the char sequence is empty.
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val empty = ""
val emptyOrNull: String? = empty.ifEmpty { null }
println(emptyOrNull) // null
val emptyOrDefault = empty.ifEmpty { "default" }
println(emptyOrDefault) // default
val nonEmpty = "abc"
val sameString = nonEmpty.ifEmpty { "def" }
println("nonEmpty === sameString is ${nonEmpty === sameString}") // true
//sampleEnd
}
© 2010–2019 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/if-empty.html