I am trying to ignore the ambiguity in your original question and read it literally. Here is an extension method that overloads TrimEnd to take a string.
static class StringExtensions { public static string TrimEnd(this string s, string remove) { if (s.EndsWith(remove)) { return s.Substring(0, s.Length - remove.Length); } return s; } }
Here are some tests showing that it works:
Debug.Assert("abc".TrimEnd("<br>") == "abc"); Debug.Assert("abc<br>".TrimEnd("<br>") == "abc"); Debug.Assert("<br>abc".TrimEnd("<br>") == "<br>abc");
I want to point out that this solution is easier to read than regex, perhaps faster than regex (you should use a profiler rather than speculation if you're concerned about performance), and is useful for removing other things from line ends.
regex becomes more appropriate if your problem is more general than what you stated (for example, if you want to remove <BR> and </BR> and deal with trailing spaces or something else.
Jay bazuzi
source share