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
strip()removes whitespace from both ends.upper()andlower()return new strings, leaving the original alone.replaceswaps every occurrence of one substring for another.
Keywords and builtins used here
Stringdefmainprintvar
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.
Step 2 of 5 in Strings, step 19 of 26 in Language basics.