List and strings

hey, how can one add a string value into a list.
for example “206” adding it to [1, 2, 4, 5, 6].

Thanks :slight_smile:

At the end or at the beginning?

at the end

and how do i convert the same string in to a list?

“206” as a string into [206] as a list

You can use https://hexdocs.pm/elixir/Kernel.html#++/2 and do

[1, 2, 4, 5, 6] ++ ["206"]

Take a note that adding at the end is often an expensive operation in Elixir since the lists are represented as linked lists under the hood

If you want to add at a concrete positions use https://hexdocs.pm/elixir/List.html#insert_at/3
At the beginning use ["206" | [1, 2, 4, 5, 6]]

2 Likes

You can do "206" |> String.to_integer() |> List.wrap()

2 Likes

how to cast a list in one column in the Db