The .NET web system I'm working on allows the end user to input HTML formatted text in some situations. In some of those places, we want to leave all the tags, but strip off any trailing break tags (but leave any breaks inside the body of the text.)
What's the best way to do this? (I can think of ways to do this, but I'm sure they're not the best.)
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.
Here are some tests to show that it works:
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.Small change to bdukes code, which should be faster as it doesn't backtrack.
As @Mitch said,
You can use a regex to find and remove the text with the regex match set to anchor at the end of the string.
you can use RegEx or check if the trailing string is a break and remove it
I'm sure this isn't the best way either, but it should work unless you have trailing spaces or something.