Library reference

Check for empty text

An empty string has no characters. A space is still a character.

String.byte_length

Simple example

Is a string with no characters empty?

Start''
ResultTrue
lesson.mojo
def main() raises:
    print(String("").byte_length() == 0)
Output
True
Advertisements

Everyday examples

A library card has no name entered. Should the form ask for one?

Start''
ResultTrue
lesson.mojo
def main() raises:
    print(String("").byte_length() == 0)
Output
True

A passenger typed a space in the seat field. Is the raw field empty?

Start' '
ResultFalse
lesson.mojo
def main() raises:
    print(String(" ").byte_length() == 0)
Output
False

The classroom screen says Ready. Is the message empty?

Start'Ready'
ResultFalse
lesson.mojo
def main() raises:
    print(String("Ready").byte_length() == 0)
Output
False
Run the example
Terminal
mojo lesson.mojo
Official documentation