Library reference

Find text inside a string

Look for a piece of text anywhere in a longer string. These examples match the letters exactly.

String.__contains__

Simple example

Does abc contain b?

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

Everyday examples

Does a ticket marked blue-zone include the word blue?

Start'blue-zone' · 'blue'
ResultTrue
lesson.mojo
def main() raises:
    print("blue" in String("blue-zone"))
Output
True

Does a shelf marked fruit contain the word milk?

Start'fruit' · 'milk'
ResultFalse
lesson.mojo
def main() raises:
    print("milk" in String("fruit"))
Output
False

Does the route name East Road include Road?

Start'East Road' · 'Road'
ResultTrue
lesson.mojo
def main() raises:
    print("Road" in String("East Road"))
Output
True
Run the example
Terminal
mojo lesson.mojo
Official documentation