isNullOrBlank
Returns true
if this nullable char sequence is either null
or empty or consists solely of whitespace characters.
Returns true
if this nullable char sequence is either null
or empty or consists solely of whitespace characters.
import java.util.Locale import kotlin.test.* fun main() { //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 }
xxxxxxxxxx
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
Thanks for your feedback!