Strings
August 22, 2023 1 minutes • 150 words
Table of contents
Elixir strings are a sequence of bytes.
string = <<104,101,108,108,111>>
"hello"
<< >>
tells the compiler that its elements are bytes.
Charlists
Elixir “strings” are represented with a sequence of bytes, not by an array of characters.
In Elixir ‘char lists’, each value is the Unicode code point.
You can get a character’s code point by using ?
Charlist support is mainly included because it is required for some Erlang modules.
?Z
# 90
# This allows you to use the notation ?Z rather than ‘Z’ for a symbol.
String Function | Description |
---|---|
.length/1 |
Returns the number of Graphemes |
.replace/3 |
Replaces a current pattern in the string |
.duplicate/2 |
Repeats string n times |
.split/2 |
Splits up the string based on the char |
String.length "Hello"
# 5
String.replace("string", "char_to_replace", "char_to_put")
String.replace("Hello", "e", "a")
# "Hallo"
String.duplicate("Oh my ", 3)
# "Oh my Oh my Oh my "
String.split("Hello World", " ")
["Hello", "World"]