The String module in Elixir
Six transformations of a workshop motto, one String function at a time.
# The String module works on UTF-8 text, one graceful function at a time.
motto = " measure twice, cut once "
clean = String.trim(motto)
IO.puts(String.upcase(clean))
IO.puts("words: #{length(String.split(clean))}")
IO.puts(String.replace(clean, "cut", "saw"))
IO.puts(String.pad_leading("total", 12, "."))
IO.puts("starts well? #{String.starts_with?(clean, "measure")}")
How it works
String.trimstrips the stray whitespace before anything else runs.splitbreaks the motto into words andreplaceswaps the cutting tool.pad_leadingdots a column into place andstarts_with?asks a yes-or-no question.
Keywords and builtins used here
IOString
The run, in numbers
- Lines
- 9
- Characters to type
- 374
- Tokens
- 102
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 82 seconds.
Step 2 of 3 in Strings & sigils, step 17 of 27 in Language basics.