Library reference

Remove spaces at both ends

Remove the spaces before and after a name. A space between two words stays in place. These examples use ordinary spaces.

String.strip

Simple example

Trim the single letter.

Start' a '
Resulta
lesson.mojo
def main() raises:
    print(String(" a ").strip())
Output
a
Advertisements

Everyday examples

Remove the extra spaces copied around a ticket number.

Start' B12 '
ResultB12
lesson.mojo
def main() raises:
    print(String("  B12 ").strip())
Output
B12

Clean up the outside spaces on a shelf label.

Start' Red Apples '
ResultRed Apples
lesson.mojo
def main() raises:
    print(String(" Red Apples  ").strip())
Output
Red Apples

Clean up a name copied from a signup sheet.

Start' Ana '
ResultAna
lesson.mojo
def main() raises:
    print(String("  Ana ").strip())
Output
Ana
Run the example
Terminal
mojo lesson.mojo
Official documentation