typestar

String methods in Mojo

Trimming and case conversion ship with the standard String.

def main():
    var raw = String("   Mojo Lang   ")
    var trimmed = raw.strip()
    print("[" + trimmed + "]")
    print(trimmed.upper())
    print(trimmed.lower())
    print(trimmed.replace(" ", "-"))

How it works

  1. strip() removes whitespace from both ends.
  2. upper() and lower() return new strings, leaving the original alone.
  3. replace swaps every occurrence of one substring for another.

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
179
Tokens
56
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 40 seconds.

Type this snippet

Step 2 of 5 in Strings, step 19 of 26 in Language basics.

← Previous Next →

String methods in other languages