ifBlank
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.
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 java.util.Locale import kotlin.test.* fun main() { //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 }
xxxxxxxxxx
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
Thanks for your feedback!