Визначення довжини рядка

Elixir:
Визначення довжини рядка

How to:

(Як зробити:)

In Elixir, we use the String.length/1 function. Here’s how:

my_string = "Привіт, світ!"
length = String.length(my_string)
IO.puts(length)

Output:

13

Deep Dive

(Поглиблене занурення:)

Finding the length of a string is straightforward but it’s worth noting that Elixir handles Unicode correctly, counting graphemes, not bytes. Historically, not all programming languages did this, which led to inaccurate lengths with non-ASCII characters.

For alternatives, we might consider byte_size/1, but this measures raw bytes, not characters, so it’s different.

Understanding the implementation, Elixir’s usage of the BEAM VM means it’s built with Unicode awareness from the ground up, ensuring accurate string length measurements regardless of the language used.

See Also

(Див. також:)