typestar

Strings and binaries in Erlang

Char lists for tradition, binaries for bytes.

%% Strings are char lists; binaries are the modern byte strings.
main(_) ->
    Name = "Kettle Street",
    io:format("~s has ~p characters~n", [Name, length(Name)]),
    io:format("Upper: ~s~n", [string:uppercase(Name)]),

    Bin = <<"exchange">>,
    io:format("Binary size: ~p~n", [byte_size(Bin)]),

    %% Binaries pattern-match by segments.
    <<First, Rest/binary>> = Bin,
    io:format("First byte ~p, rest ~s~n", [First, Rest]),

    Joined = string:join(["patch", "test", "ring"], " > "),
    io:format("~s~n", [Joined]).

How it works

  1. A string is a list: length/1 counts its characters.
  2. <<"exchange">> is a binary; byte_size measures it.
  3. <<First, Rest/binary>> pattern-matches by segments.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
493
Tokens
154
Three-star pace
60 tpm

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

Type this snippet

Step 1 of 4 in Text & numbers, step 19 of 31 in Language basics.

← Previous Next →