Skip to content

Commit

Permalink
Update binaries-strings-and-charlists.md (#12850)
Browse files Browse the repository at this point in the history
  • Loading branch information
magashpillay authored Aug 9, 2023
1 parent f2fe43e commit dced276
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/elixir/pages/getting-started/binaries-strings-and-charlists.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ iex> [?h, ?e, ?l, ?l, ?o]
~c"hello"
```

The `~c` sigil (we'll cover sigils later in the ["Sigils"](sigils.md) chapter)
indicates the fact that we are dealing with a charlist and not a regular string.
The `~c` sigil (we'll cover sigils later in the ["Sigils"](sigils.md) chapter) indicates the fact that we are dealing with a charlist and not a regular string.

Instead of containing bytes, a charlist contains integer code points. However, the list is only printed as a sigil if all code points are within the ASCII range:

Expand All @@ -258,7 +257,14 @@ iex> heartbeats_per_minute = [99, 97, 116]
~c"cat"
```

You can convert a charlist to a string and back by using the `to_string/1` and `to_charlist/1` functions:
You can always for charlists to be printed in their list representation by calling the `inspect/2` function:

```

Check failure on line 262 in lib/elixir/pages/getting-started/binaries-strings-and-charlists.md

View workflow job for this annotation

GitHub Actions / Lint Markdown content

Fenced code blocks should have a language specified

lib/elixir/pages/getting-started/binaries-strings-and-charlists.md:262 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.29.0/doc/md040.md
iex> inspect(heartbeats_per_minute, charlists: :as_list)
"[99, 97, 116]"
```

Furthermore, you can convert a charlist to a string and back by using the `to_string/1` and `to_charlist/1`:

```elixir
iex> to_charlist("hełło")
Expand All @@ -271,7 +277,7 @@ iex> to_string(1)
"1"
```

Note that those functions are polymorphic - not only do they convert charlists to strings, they also operate on integers, atoms, and so on.
The functions above are polymorphic, in other words, they accept many shapes: not only do they convert charlists to strings (and vice-versa), they can also convert integers, atoms, and so on.

String (binary) concatenation uses the `<>` operator but charlists, being lists, use the list concatenation operator `++`:

Expand Down

0 comments on commit dced276

Please sign in to comment.