Remove last specific character in a string c#

I use WinForms c#.I have string value like below,

string Something = "1,5,12,34,";

I need to remove last comma in a string. So How can i delete it ?


Try string.TrimEnd():

Something = Something.TrimEnd(',');

King King's answer is of course correct, and Tim Schmelter's comment is also good suggestion in your case.

But if you really want to remove the last comma in a string, you should find the index of the last comma and remove it like this:

string s = "1,5,12,34,12345";
int index = s.LastIndexOf(',');
Console.WriteLine(s.Remove(index, 1));

Output will be:

1,5,12,3412345

Here is a demonstration.

It is unlikely that you want this way but I want to point it out. And remember, the String.Remove method doesn't remove any characters in the original string, it returns new string.


Try string.Remove();

string str = "1,5,12,34,";
string removecomma = str.Remove(str.Length-1);
MessageBox.Show(removecomma);