Posted By

ecavazos on 01/18/08


Tagged

array String loop foreach comma delimit


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
vali29
tux-world


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  

Comments

RSS Icon Subscribe to comments
Posted By: dragthor on January 6, 2009

Check out the String.Join method... does the same thing... probably faster and more efficient:

http://msdn.microsoft.com/en-us/library/system.string.join.aspx

  • Kris Krause

You need to login to post a comment.