Kotlin: Difference between Blank and Empty


Kotlin String has methods, isBlank() and isEmpty(). I wrote about their difference.

Environment

  • Kotlin 1.2.0

Empty

Empty means no-content. isEmpty is the same as comparing to zero string (value == "").

In definition of the method, it calculate String’s length and compares it to zero.

isEmpty 実行例

Blank

Blank means non-filled, not-printed. isBlank returns true for invisible characters, also for wide space and CR, LF, and so on.

Internally, it uses isWhitespace method.

And Kotlin isWhitespace method uses Java’s methods, isWhitespace and isSpaceChar.

isBlank 実行例

Original Meaning of Empty and Blank

Empty

Vacent. Describes when there is nothing inside(content). Used in emotional/content-related/idea-related context.

empty bottle, empty bank account

Blank

Nothing to see. Describes facial expression/missing surface content.

blank form, blank sheet, blank piece of paper, blank computer screen

empty and blank are adjective. So emptier, emptiest, blanker, blankest are valid words, but they are not used frequently.

Related Methods

Kotlin prepares methods similar to isEmpty and isBlank.

  • isNotEmpty
  • isNullOrEmpty
  • isNotBlank
  • isNullOrBlank