Kotlin isBlank
Determine whether a field supplies any content after ignoring ordinary whitespace.
A simple example
isBlank
- Start with " "
- Do this Check for blank text
- Result Yes
fun main() {
val value = " "
println(if (value.isBlank()) "yes" else "no")
}
yes
Syntax
value.isBlank()
Run these examples in Kotlin
Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.
kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jarCheck an attendance comment
A form accepts an optional note typed or pasted by a customer or student. Decide whether the field contains meaningful text before displaying it to the person who will act on the request.
fun main() {
val value = " \t "
println(if (value.isBlank()) "yes" else "no")
}
yes
Spaces and a tab supply no comment.
Check an aisle request
A traveler types a seating preference with padding around the word aisle. Check whether the preference still contains useful text after the surrounding whitespace is removed.
fun main() {
val value = " aisle "
println(if (value.isBlank()) "yes" else "no")
}
no
The word aisle is meaningful after trimming.
Review a space-only answer
A student pressed the space bar three times before submitting a response. The submission form treats whitespace-only text as a blank response. Which status should the reviewer see?
What to watch for
Blankness is a field policy. Choose which Unicode whitespace characters the application should ignore.