I'm 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 to show 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, probably faster than regex (you should use a profiler, not speculation, if you're concerned about performance), and useful for removing other things from the ends of strings.
regex becomes more appropriate if your problem is more general than you stated (e.g., if you want to remove <BR>
and </BR>
and deal with trailing spaces or whatever.