Library reference

Match the end of a string

Check the letters at the end of the text. The result is true only when those letters match.

String.endswith

Simple example

Does abc end with c?

Start'abc' · 'c'
ResultTrue
lesson.mojo
def main() raises:
    print(String("abc").endswith("c"))
Output
True
Advertisements

Everyday examples

A teacher wants PDF files. Does this name end in .pdf?

Start'notes.pdf' · '.pdf'
ResultTrue
lesson.mojo
def main() raises:
    print(String("notes.pdf").endswith(".pdf"))
Output
True

A music player lists MP3 files. Does the recording have that ending?

Start'rehearsal.wav' · '.mp3'
ResultFalse
lesson.mojo
def main() raises:
    print(String("rehearsal.wav").endswith(".mp3"))
Output
False

Fragile shelf labels end in -F. Does this one belong there?

Start'12-F' · '-F'
ResultTrue
lesson.mojo
def main() raises:
    print(String("12-F").endswith("-F"))
Output
True
Run the example
Terminal
mojo lesson.mojo
Official documentation