kotlin : what is difference between isNullOrEmpty and isNullOrBlank?

isNullOrEmpty()

  • returns true for a string with no characters and/or zero length as @Wyck commented.
  • returns false for whitespace.

isNullOrBlank()

  • returns true for a string with no characters and/or zero length. Same as isNullOrEmpty()
  • and will ALSO return true for whitespace. Different than isNullOrEmpty().

isNullOrBlank() takes whitespace into account:

fun main() {
  val thisIsBlank = "   "

  println(thisIsBlank.isNullOrEmpty())
  println(thisIsBlank.isNullOrBlank())
}

This prints:

false
true

because thisIsBlank is not empty, but it is blank.

Tags:

Kotlin