Reply to comment

Programming Puzzle: What is the best way to format a comma delimited list?

Say you have a list, array, vector, storage format doesn't matter:

std::vector<int> vec;
vec.push_back(1);
vec.push_back(17);
vec.push_back(9);

And you want to format this list into a comma delimited string without the trailing comma, what is the best way to do this?

Example output:

1,17,9

In any language, with any list type. Post your answers here. Be sure to use code formatting for your answer, to make it easier for everyone to read.

<code language="lua">
--my code solution
</code>

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
16 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.