How to comma-separate a set of strings without the final comma

asp.net, c#

Solution

Add them to a collection then use `string.Join`:

var list = new List<string>();

foreach (GridViewRow row in GridViewTotalManpower.Rows) {
    // ...other code here...
    list.Add(Convert.ToString(GridViewTotalManpower.DataKeys[rowPosition]["WorkerName"].ToString()));
}

 hidfWorker.Value = string.Join(", ", list);

Problem

``` int rowPosition = 0; string WorkerName = ""; DataTable dtAllotedManpower = new DataTable(); dtAllotedManpower.Columns.Add("WorkerName"); foreach (GridViewRow row in GridViewTotalManpower.Rows) { if (row.RowType == DataControlRowType.DataRow) { DataRow drAllotedManpower = dtAllotedManpower.NewRow(); CheckBox chkChild = (CheckBox)GridViewTotalManpower.Rows[rowPosition].FindControl("chkChild"); if (chkChild.Checked == true) { WorkerName = Convert.ToString(GridViewTotalManpower.DataKeys[rowPosition]["WorkerName"].ToString()) + "," + WorkerName; } rowPosition++; } hidfWorker.Value = WorkerName; ``` I have Written the following piece of code. My hidden field values are coming like this "HARSH,RIMA," But i want the value "HARSH,RIMA" (without ',' after the last word). how to construct the code for that ? . there will be no 'comma' after last word .

Original source