W3cubDocs

/Kotlin

isNullOrBlank

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun CharSequence?.isNullOrBlank(): Boolean

Returns true if this nullable char sequence is either null or empty or consists solely of whitespace characters.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
fun validateName(name: String?): String {
    if (name.isNullOrBlank()) throw IllegalArgumentException("Name cannot be blank")
    // name is not nullable here anymore due to a smart cast after calling isNullOrBlank
    return name
}

println(validateName("Adam")) // Adam
// validateName(null) //  will fail
// validateName("") //  will fail
// validateName("  \t\n") //  will fail
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-blank.html