ifEmpty
inline
fun
<
C
,
R
>
C
.
ifEmpty
(
defaultValue
:
(
)
->
R
)
:
R
where
C
:
CharSequence
,
C
:
R
(source)
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(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
}