user1907849
user1907849

Reputation: 980

String separating in list

I have a list called listitems which contains information on items.

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

But when I use the following I get the output as :

",a,b" which is incorrect

but I want the output as "a,b".

How can I modify the code ?

foreach(var a in listitems)
{
  gh = gh +"," + a;
}

Upvotes: 4

Views: 108

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

You can use with String.Join method.

Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member.

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

Upvotes: 4

L.B
L.B

Reputation: 116108

string gh = String.Join(",", listitems); //

Upvotes: 9

Related Questions