typestar

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

  1. String.trim strips the stray whitespace before anything else runs.
  2. split breaks the motto into words and replace swaps the cutting tool.
  3. pad_leading dots a column into place and starts_with? asks a yes-or-no question.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Strings & sigils, step 17 of 27 in Language basics.

← Previous Next →