ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Strip Html

Removes any HTML or XML tags from a string. Super simple, but I didn't see anything here like it. I've created similar methods in the past to take into account things like <script> blocks, but I'm not worrying about that here.

Source

// Used when we want to completely remove HTML code and not encode it with XML entities.
public static string StripHtml(this string input)
{
    // Will this simple expression replace all tags???
    var tagsExpression = new Regex(@"</?.+?>");
    return tagsExpression.Replace(input, " ");
}

Example

var htmlText = "<p>Here is some text. <span class="bold">This is bold.</span> Talk to you later.</p>;
var cleanString = htmlText.StripHtml();

Author: Jason Norris

Submitted on: 3 mei 2011

Language: C#

Type: System.String

Views: 19307