ifEmpty

inline fun <C : CharSequence, R, R> C.ifEmpty(defaultValue: () -> R): R

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 samples.*
import java.util.Locale
import kotlin.test.*
fun main() { 
   //sampleStart 
   val empty = ""

val emptyOrNull: String? = empty.ifEmpty { null }
assertPrints(emptyOrNull, "null")

val emptyOrDefault = empty.ifEmpty { "default" }
assertPrints(emptyOrDefault, "default")

val nonEmpty = "abc"
val sameString = nonEmpty.ifEmpty { "def" }
assertTrue(nonEmpty === sameString) 
   //sampleEnd
}