ifEmpty
Returns this char sequence if it's not empty or the result of calling defaultValue function if the char sequence is empty.
Returns this char sequence if it's not empty or the result of calling defaultValue function if the char sequence is empty.
import java.util.Locale import kotlin.test.* fun main() { //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 }
xxxxxxxxxx
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
Thanks for your feedback!