Every once in a while, I need to build a character separated string while looping through a collection. Believe it or not, there is always that first or last separator character that gets in the way! 🙂 Usually, I end up chopping off the extra character, which is another line of code. I could leave with that, but out of curiosity, does anyone have any cool “smooooth” way of doing this? (in C# and/or JavaScript)
Example:
{"Joe", "Jane", "Jim"}
After building comma separated string, we get:
"Joe, Jane, Jim, " or ", Joe, Jane, Jim"
Looking for a cool way to build
"Joe, Jane, Jim"
without any string “chopping” after.
In Javascript it’s easy:
Most languages have some form of “join” either built-in or in some library:
By the way, if you are writing such a function, you should just use a check to not prepend the “glue” on the first pass, rather than chopping the string afterward: