mlen
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!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cjbottaro
I’d just use string interpolation.
Or string concatenation if you prefer.
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.
t0t0
Hi,
I was wondering if Kernel.inspect/1 would feet? (mostly in case a
"needs to be escaped according to Elixir syntax)Also, it is possible to regroup the Enum.map + Enum.join operations in the previous answer with
Enum.map_join/3al2o3cr
That’s a bummer, because this is a perfect situation to use the “split on Regex” feature of
String.split:Some extension-points / places to customize the code:
the
trim: trueoption toString.splitprevents an empty""at the end of the result if there’s a trailing,on the input. Your example didn’t specify what the behavior should be.if the input values might contain
", you’ll want to make theEnum.mapstep smarter about escapingcommas in the input that aren’t followed by at least one space will be passed through unchanged. Whether that’s a bug or a feature depends on your expectations for the input.
hst337
NIT:
Enum.map_joinmlen
My requirements are very basic for now… However the regex is not preferred as if when the solution will later require that the string does not have a trailing comma, has a underscore in between words, may contain minimum two dots etc, then the regex itself becomes too much burden to think of (you can tell I’m a big fan of regex ;-])
BTW your solution does not return each word in double quotes. Corrected: