Member-only story

Get a Comma Separated List From An Array

Stop writing rubbish!

Steven Curtis
Oct 23, 2020
Photo by The New York Public Library on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Here is a video version of this article

Not great

Here I can loop through the input Strings, adding the comma to each element in turn.

But there is a wrinkle in this! The last element would have a comma appended to the end! This is then removed with a quick outputString.removeLast().

The potential function is shown here:

Better

We can use a map combined with String interpolation to join the elements with a comma.

Here is the end result:

--

--

No responses yet