Trim string from the end of a string in .NET - why is this missing?

前端 未结 13 1341
夕颜
夕颜 2020-12-13 12:08

I need this all the time and am constantly frustrated that the Trim(), TrimStart() and TrimEnd() functions don\'t take strings as inputs. You call EndsWith() on a string, an

13条回答
  •  执笔经年
    2020-12-13 12:30

    EDIT - wrapped up into a handy extension method:

    public static string TrimEnd(this string source, string value)
    {
        if (!source.EndsWith(value))
            return source;
    
        return source.Remove(source.LastIndexOf(value));
    }
    

    so you can just do s = s.TrimEnd("DEF");

提交回复
热议问题