ifBlank

inline fun <C : CharSequence, R, R> C.ifBlank(defaultValue: () -> R): 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.

Since Kotlin

1.3

Samples

import samples.*
import java.util.Locale
import kotlin.test.*
fun main() { 
   //sampleStart 
   val blank = "    "

val blankOrNull: String? = blank.ifBlank { null }
assertPrints(blankOrNull, "null")

val blankOrDefault = blank.ifBlank { "default" }
assertPrints(blankOrDefault, "default")

val nonBlank = "abc"
val sameString = nonBlank.ifBlank { "def" }
assertTrue(nonBlank === sameString) 
   //sampleEnd
}