[c#] Delete last char of string

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.

This question is related to c# string char

The answer is


string strgroupids = string.Empty;

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

strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

Note that the use of ForEach here is normally considered "wrong" (read for example http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx)

Using some LINQ:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => p + q + ',');
strgroupids = strgroupids.Substring(0, str1.Length - 1);

Without end-substringing:

string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => (p != string.Empty ? p + "," + q : q.ToString()));

What about doing it this way

strgroupids = string.Join( ",", groupIds );

A lot cleaner.

It will append all elements inside groupIds with a ',' between each, but it will not put a ',' at the end.


Strings in c# are immutable. When in your code you do strgroupids.TrimEnd(','); or strgroupids.TrimEnd(new char[] { ',' }); the strgroupids string is not modified.

You need to do something like strgroupids = strgroupids.TrimEnd(','); instead.

To quote from here:

Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.


Additional to sll's solution: It's better to trim the string in case there are some blank(s) at the end.

strgroupids = strgroupids.Remove(strgroupids.Trim().Length - 1);

In C# 8 ranges and indices were introduced, giving us a new more succinct solution:

strgroupids = strgroupids[..^1];

string.Join is better, but if you really want a LINQ ForEach:

var strgroupids = string.Empty;

groupIds.ForEach(g =>
{
    if(strgroupids != string.Empty){
        strgroupids += ",";
    }

    strgroupids += g;
});

Some notes:

  • string.Join and foreach are both better than this, vastly slower, approach
  • No need to remove the last , since it's never appended
  • The increment operator (+=) is handy for appending to strings
  • .ToString() is unnecessary as it is called automatically when concatenating non-strings
  • When handling large strings, StringBuilder should be considered instead of concatenating strings

There is no "quick-and-dirty" way of doing this. I usually do:

mystring= string.Concat(mystring.Take(mystring.Length-1));

Removes any trailing commas:

while (strgroupids.EndsWith(","))
    strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);

This is backwards though, you wrote the code that adds the comma in the first place. You should use string.Join(",",g) instead, assuming g is a string[]. Give it a better name than g too !


Add an extension method.

public static string RemoveLast(this string text, string character)
{
    if(text.Length < 1) return text;
    return text.Remove(text.ToString().LastIndexOf(character), character.Length);
}

then use:

yourString.RemoveLast(",");

As an alternate to adding a comma for each item you could just using String.Join:

var strgroupids = String.Join(",",  groupIds);

This will add the seperator ("," in this instance) between each element in the array.