Background: Users too often add extra line breaks, which can vary between a BR, P and DIV tags to the end (and sometimes the start) of their input via a WYSIWYG editor. I need to do a clean up to remove any type of line break from the start and end of the input.

Here is an example of input that requires cleaning:

```html
<div>&nbsp;</div><div>&nbsp;</div><p>&nbsp;</p><br />this is the input to keep<div>&nbsp;</div><br /><div>&nbsp;</div><p>&nbsp;</p><div>&nbsp;</div>

```

To clean up, I created the following extension, which works well... except, it loops through each line break in order.

```cs
public static string RemoveStartAndEndBreaks( this string input )
    {
        var lineBreaks = new[] { "<br>", "<br/>", "<br />", "<p></p>", "<p> </p>", "<p>&nbsp;</p>", "<div></div>", "<div> </div>", "<div>&nbsp;</div>" };

        foreach( var lb in lineBreaks )
        {
            while( input.StartsWith( lb ) )
            {
                input = input.Substring( lb.Length );
            }

            while( input.EndsWith( lb ) )
            {
                input = input.Substring( 0, input.Length - lb.Length );
            }
        }

        return input;
    }

```

So the issue is, it will only clean all line breaks if they appear in the same order as they are presented in the array. So using the above example, the resulting output is:

```html
<p>&nbsp;</p><br />this is the input to keep<div>&nbsp;</div><br /><div>&nbsp;</div><p>&nbsp;</p>

```

Notice it only removes the first break type it finds if it appears at the start or end of the input string - and ignores the rest.

Q1. How do you restart the loop through the array when a match is found?

Q2. Is restarting the loop every time a match is found the only option or is there a more efficient way to check for each occurrence and remove it from the start and the end?

Q3. Have I missed something glaringly obvious?

Please note that the input is html and therefore the resulting output will (potentially) contain the same line breaks described in the string array, which must remain.

i.e. I am only removing the line breaks from the start and end of the input. The ones in the middle need to stay there.