Elixir:
Finding the length of a string
How to:
In Elixir, you get a string’s length with the String.length/1
function. Here’s how:
my_string = "Hello, World!"
length = String.length(my_string)
IO.puts(length)
Sample output:
13
Deep Dive
Internally, Elixir strings are UTF-8 encoded binaries. Each character can be one to four bytes. So, when we call String.length/1
, we’re not merely counting bytes; we’re counting Unicode graphemes, which are what we perceive as characters.
Historically, string length operations in many languages were byte-centric and did not account well for multi-byte characters. Elixir’s approach is modern and Unicode-friendly from the get-go.
As for alternatives, you could manually count graphemes using recursion or with a loop, but that’s unnecessary and inefficient. String.length/1
is optimized and idiomatic.
Elixir’s implementation uses an Erlang NIF (Native Implemented Function) for String.length/1
, making it lightning fast. Counting bytes instead of graphemes is done with byte_size/1
, which counts the raw bytes of a string’s binary representation—useful in low-level operations where encoding doesn’t matter.