kotlin : what is difference between isNullOrEmpty and isNullOrBlank?

Viewed 9781

I want check value of my EditText in android so I saw two function for my String value :

user.isNullOrBlank()

and

user.isNullOrEmpty()

what is difference between them?

2 Answers

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.

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().
Related