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
- A string is a list:
length/1counts its characters. <<"exchange">>is a binary;byte_sizemeasures it.<<First, Rest/binary>>pattern-matches by segments.
Keywords and builtins used here
byte_sizeformatjoinlengthmainuppercase
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.
Step 1 of 4 in Text & numbers, step 19 of 31 in Language basics.