mlen

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

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 :wink:

Also Liked

cjbottaro

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.

hst337

hst337

NIT: Enum.map_join

Where Next?

Popular in Questions Top

jerry
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
logicmason
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
polypush135
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
alice
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
hpopp
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
Fl4m3Ph03n1x
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
Mooodi
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
lucidguppy
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
idi527
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 Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
William
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
joaquinalcerro
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
_russellb
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
Harrisonl
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
minhajuddin
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
baxterw3b
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
aesmail
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

We're in Beta

About us Mission Statement