Merging a String Array to a Comma delimited String (foreach)


/ Published in: C#
Save to your folder(s)

This can be useful for string preparations.


Copy this code and paste it in your HTML
  1. string str = "";
  2.  
  3. // loop though an array and create a string
  4. // delimited with commas
  5. foreach (string s in fieldNames)
  6. {
  7. str += s;
  8.  
  9. // if this not the last item in the array
  10. // append a comma to end of the string.
  11. if (s != fieldNames[fieldNames.Length - 1])
  12. {
  13. str += ", ";
  14. }
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.