ifEmpty (original) (raw)

Returns this char sequence if it's not empty or the result of calling defaultValue function if the char sequence is empty.

Since Kotlin

1.3

Samples

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
}