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

前端 未结 13 1351
夕颜
夕颜 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:42

    I knocked up this quick extension method.

    Not positive it works (I can't test it right now), but the theory is sound.

        public static string RemoveLast(this string source, string value)
        {
            int index = source.LastIndexOf(value);
            return index != -1 ? source.Remove(index, value.Length) : source;
        }
    

提交回复
热议问题