Is a String empty, blank or whitespace in EK9?

← Common String Operations · Ref: Q2284

EK9 asks three different questions about text with nothing in it, and they give different answers for a String such as " ":

  noText <- text is empty        //length 0: only ""
  blank <- text.isBlank()        //"" or only whitespace
  spaces <- text.isWhitespace()  //at least one character, all whitespace

EMPTY

'empty' (written 'text is empty' or 'text.empty()') is true only for "", the String with no characters. " " has one character, so it is NOT empty. That is what empty means in every other language, and what it means for a List (no items).

BLANK

isBlank() is true for "" or for text that is only whitespace: "", " ", "\t
". It is the test for typed input with nothing in it:

  hasContent() as pure
    -> typed as String
    <- rtn as Boolean: not typed.isBlank()

For spaces, tabs and newlines it gives the same answer as 'text.trim() is empty'.

WHITESPACE

isWhitespace() needs at least one character, and every character must be whitespace: " " is whitespace, "" is not. For one Character, 'character.isWhitespace()' sits beside isAlpha(), isDigit(), isUpperCase() and isLowerCase().

WHICH CHARACTERS ARE WHITESPACE

Java's Character.isWhitespace: space, tab, newline, carriage return, form feed, vertical tab, the separators U+001C to U+001F, and the Unicode space, line and paragraph separators (such as the em space U+2003). The no-break spaces U+00A0, U+2007 and U+202F are NOT whitespace, so "\u00A0".isBlank() is false. Python's isspace() and Kotlin's isWhitespace() count the no-break space as whitespace; EK9 follows Java.

SET IS A DIFFERENT QUESTION

'text?' asks whether the String has a value at all. "" is SET: its value is known, it has no characters. An UNSET String, String(), gives an UNSET answer to all three tests, and an if skips an unset condition - so ask '?' first when the text may be missing (see Q1480).

See Q170 for length and empty. See Q173 for trim. See Q1480 for an unset String and 'is empty'.

Example

#!ek9
defines module qa.stringops.emptyblankwhitespace

  defines function

    shown() as pure
      -> answer as Boolean
      <- rtn as String: answer? <- $answer : "unset"

    report() as pure
      ->
        label as String
        text as String
      <- rtn as String: `${label}: empty=${shown(text is empty)} isBlank=${shown(text.isBlank())} isWhitespace=${shown(text.isWhitespace())} set=${text?}`

    hasContent() as pure
      -> typed as String
      <- rtn as Boolean: not typed.isBlank()

  defines program

    EmptyBlankOrWhitespace()
      stdout <- Stdout()
      stdout.println(report("nothing", ""))
      stdout.println(report("spaces", "   "))
      stdout.println(report("tab and newline", "\t\n"))
      stdout.println(report("padded word", " hi "))
      stdout.println(report("no-break space", "\u00A0"))
      stdout.println(report("unset", String()))

      tab <- '\t'
      letter <- 'x'
      stdout.println(`tab is whitespace: ${tab.isWhitespace()}`)
      stdout.println(`x is whitespace: ${letter.isWhitespace()}`)

      for typed in ["Alice", "   ", ""]
        stdout.println(`[${typed}] has content: ${hasContent(typed)}`)

Common mistakes

E50060 — EK9's String has no isEmpty(): 'is empty' (or empty()) is the length-0 test, and isBlank() is the test for text with nothing but whitespace in it. See ek9 -h E50060 for details.

Incorrect:

      <- rtn as Boolean: not typed.isEmpty()

Correct:

      <- rtn as Boolean: not typed.isBlank()
Other ways to ask this
  • Is " " empty in EK9?
  • What is the difference between empty and isBlank in EK9?
  • How do I check that a String is blank or only whitespace?
  • Does EK9 have isBlank like Java and Kotlin?
  • How do I test whether a Character is whitespace?

Coming from another language?

Java: isEmpty() is length 0; isBlank() (Java 11) is empty or only whitespace. Kotlin: isEmpty(), isBlank(), Char.isWhitespace(). C#: s.Length == 0, string.IsNullOrWhiteSpace(s). Python: 'not s' for empty, s.isspace() for at least one character all whitespace, 'not s.strip()' for blank. Swift: s.isEmpty. Rust: s.is_empty(), s.trim().is_empty(). Go: len(s) == 0, strings.TrimSpace(s) == "". JavaScript: s.length === 0, s.trim() === ''. EK9: 'text is empty' (length 0), text.isBlank(), text.isWhitespace() and Character's isWhitespace(); an unset String gives an unset answer to each.

Keywords: Character, String, blank, empty, is empty, isBlank, isEmpty, isWhitespace, isspace, no-break space, trim, whitespace