-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update binaries-strings-and-charlists.md (#12850)
- Loading branch information
1 parent
f2fe43e
commit dced276
Showing
1 changed file
with
10 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
||
|
@@ -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 GitHub Actions / Lint Markdown contentFenced code blocks should have a language specified
|
||
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") | ||
|
@@ -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 `++`: | ||
|
||
|