We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

ecavazos on 01/18/08


Tagged

array String loop foreach comma delimit


Versions (?)


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


Published in: C# 


This can be useful for string preparations.


  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 

You need to login to post a comment.