typestar

Splitting strings in Erlang

string:split and lexemes turn ledger lines into data.

%% string:split and lexemes turn a ledger line into data.
main(_) ->
    Entry = "5560199,12,night",

    [Number, Mins, Shift] = string:split(Entry, ",", all),
    io:format("~s spoke ~s minutes on the ~s shift~n",
              [Number, Mins, Shift]),

    %% lexemes splits on any of several separators.
    Note = "patch 3;test 7 ring 9",
    Words = string:lexemes(Note, "; "),
    io:format("~p instructions~n", [length(Words)]),
    [io:format("- ~s~n", [W]) || W <- Words].

How it works

  1. string:split(Entry, ",", all) breaks every comma.
  2. The result destructures straight into three names.
  3. lexemes splits on any of several separator characters.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
435
Tokens
118
Three-star pace
60 tpm

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

Type this snippet

Step 3 of 4 in Text & numbers, step 21 of 31 in Language basics.

← Previous Next →