Library contents

Scala String.isBlank

Determine whether a field supplies any content after ignoring ordinary whitespace.

A simple example

String.isBlank

  1. Start with " "
  2. Do this Check for blank text
  3. Result Yes
Removing the spaces leaves no visible content.
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = "   "
        println(if (value.isBlank()) "yes" else "no")
    }
}
Output
yes

Syntax

Scala syntax
value.isBlank()
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

Check 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.

From the programval value = " \t "
Resultyes
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = " \t "
        println(if (value.isBlank()) "yes" else "no")
    }
}
Output
yes

Spaces and a tab supply no comment.

Advertisements

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.

From the programval value = " aisle "
Resultno
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val value = " aisle "
        println(if (value.isBlank()) "yes" else "no")
    }
}
Output
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?

GivenThree ASCII spaces
Worked resultBlank

What to watch for

Blankness is a field policy. Choose which Unicode whitespace characters the application should ignore.

Documentation for String.isBlank