Why string.TrimEnd not removing only last character in string

c#, string, trim

Solution

Why it removes all the 3 commas after 0?

Because that's what it's documented to do:

Return value: The string that remains after all occurrences of the characters in the `trimChars` parameter are removed from the end of the current string.

If you only want the final character removed, you could use:

if (str.EndsWith(","))
{
    str = str.Substring(0, str.Length - 1);
}

Or equivalently:

if (str.EndsWith(","))
{
    str = str.Remove(str.Length - 1);
}

Problem

I have string as below ``` 2,44,AAA,BBB,1,0,,, ``` So now i want to remove only last comma in above string. So i want output as ``` 2,44,AAA,BBB,1,0,, ``` I decided to use TrimeEnd as below ``` str.ToString.TrimEnd(',') ``` But it removed all the commas after 0. So i get output as below ``` 2,44,AAA,BBB,1,0 ``` Why it removes all the 3 commas after 0? I just need to remove last character in string

Original source