Remove last specific character in a string c#

前端 未结 8 1400
梦谈多话
梦谈多话 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

    The TrimEnd method takes an input character array and not a string. The code below from Dot Net Perls, shows a more efficient example of how to perform the same functionality as TrimEnd.

    static string TrimTrailingChars(string value)
    {
        int removeLength = 0;
        for (int i = value.Length - 1; i >= 0; i--)
        {
            char let = value[i];
            if (let == '?' || let == '!' || let == '.')
            {
                removeLength++;
            }
            else
            {
                break;
            }
        }
        if (removeLength > 0)
        {
            return value.Substring(0, value.Length - removeLength);
        }
        return value;
    }
    

提交回复
热议问题