W3cubDocs

/Kotlin

ifBlank

Platform and version requirements: JVM (1.3), JS (1.3), Native (1.3)
inline fun <C, R> C.ifBlank(
    defaultValue: () -> R
): R where C : CharSequence, C : R

Returns this char sequence if it is not empty and doesn't consist solely of whitespace characters, or the result of calling defaultValue function otherwise.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val blank = "    "

val blankOrNull: String? = blank.ifBlank { null }
println(blankOrNull) // null

val blankOrDefault = blank.ifBlank { "default" }
println(blankOrDefault) // default

val nonBlank = "abc"
val sameString = nonBlank.ifBlank { "def" }
println("nonBlank === sameString is ${nonBlank === 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-blank.html