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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
I would like to know what is the best IDE for elixir development?
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
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
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 guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New







