Remove last specific character in a string c#

前端 未结 8 1384
梦谈多话
梦谈多话 2020-12-13 23:16

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

8条回答
  •  星月不相逢
    2020-12-13 23:26

    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.

提交回复
热议问题