Remove last specific character in a string c#

Try string.TrimEnd():

Something = Something.TrimEnd(',');

King King's answer is of course right. Also Tim Schmelter's comment is also good suggestion in your case.

But if you want really remove last comma in a string, you should find the index of last comma and remove like;

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 a demonstration.

It is too unlikely you want this way but I want to point it. And remember, String.Remove method doesn't remove any character in 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);

Tags:

C#

Trim