Delete last char of string

Kiwimoisi picture Kiwimoisi · Oct 26, 2011 · Viewed 480.7k times · Source

I am retrieving a lot of information in a list, linked to a database and I want to create a string of groups, for someone who is connected to the website.

I use this to test but this is not dynamic, so it is really bad:

string strgroupids = "6";

I want to use this now. But the string returned is something like 1,2,3,4,5,

groupIds.ForEach((g) =>
{
    strgroupids = strgroupids  + g.ToString() + ",";
    strgroupids.TrimEnd(',');
});

strgroupids.TrimEnd(new char[] { ',' });

I want to delete the , after the 5 but it's definitely not working.

Answer

sll picture sll · Oct 26, 2011
strgroupids = strgroupids.Remove(strgroupids.Length - 1);

MSDN:

String.Remove(Int32):

Deletes all the characters from this string beginning at a specified position and continuing through the last position