List sharing string

I have a list called listitems that contains information about items.

I want to separate each list item with a comma and put it in a string called gh

But when I use the following, I get output like:

",a,b" which is incorrect

but I want the result to be like "a,b" .

How can I change the code?

 foreach(var a in listitems) { gh = gh +"," + a; } 
+4
source share
2 answers
 string gh = String.Join(",", listitems); // 
+9
source

You can use the String.Join method.

Joining members of the constructed IEnumerable<T> collection, enter a String using the specified separator between each element.

 string gh = String.Join(",", listitems); 
+4
source

All Articles