mlen
Put double quotes for each string in a list of strings
Hi, y’ all!
I want to wrap each string from the list of strings in double quotes - so given a string:
my_string = "bilbao, newcastle-upon-tyne, gothenburg, munich "
I expect a nicely formatted one:
my_string = "bilbao", "newcastle-upon-tyne", "gothenburg", "munich"
For now I only came up with some lame solution (due to fever?!..)
iex> my_string = "bilbao, newcastle-upon-tyne, gothenburg, munich "
iex> IO.puts
my_string
|> String.split(",")
|> Enum.map(&String.trim/1)
|> Enum.map(&String.replace_prefix(&1, "", ~s(")))
|> Enum.map(&String.replace_suffix(&1, "", ~s(")))
|> Enum.join(", ")
"bilbao", "newcastle-upon-tyne", "gothenburg", "munich"
Requirements:
- performance is not really important here,
- output strings are same order as input strings,
- strings are utf8,
- prefer to avoid regex.
Thanks!
Marked As Solved
t0t0
Hi,
I was wondering if Kernel.inspect/1 would feet? (mostly in case a " needs to be escaped according to Elixir syntax)
"bilbao, newcastle-upon-tyne, gothenburg, munich "
|> String.split(",")
|> Stream.map(&String.trim/1)
|> Stream.map(&inspect/1)
|> Enum.join(", ")
|> IO.puts()
Also, it is possible to regroup the Enum.map + Enum.join operations in the previous answer with Enum.map_join/3 ![]()
2
Also Liked
cjbottaro
I’d just use string interpolation.
String.split(my_string, ",")
|> Enum.map(fn s ->
s = String.trim(s)
"\"#{s}\""
end)
|> Enum.join(", ")
Or string concatenation if you prefer.
String.split(my_string, ",")
|> Enum.map(fn s ->
"\"" <> String.trim(s) <> "\""
end)
|> Enum.join(", ")
Your solution is calling map many times, each doing a single step. A single call to map can do all the steps necessary, regardless of string interpolation, concatenation, or replace.
2
hst337
NIT: Enum.map_join
1
Popular in Questions
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I would like to know what is the best IDE for elixir development?
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









