StripHtml string extension

Tuesday, April 10, 2018

By: Chris Dunn

If you're working on a web project with customizable html content you will need, at some point, a way to strip the Html in order to access the raw text.   One scenario I use often is automatically generating an intro for a listing page for products, by using the html formatted product description.  I do this by cleaning the html out of the description, and then grabbing the first 255 characters to display.  In the case of extracting a preview or description of html text, I suggest you cache or persist the value to improve performance (IE: not doing this on every display).

Extension Method

I like to use extension methods when possible.  It makes the flow of code cleaner in my opinion.  So here is the StripHtml extension method.  Remember it needs to be in a public static class.

    public static class StringExtensions
    {

        public static string StripHtml(this string text)
        {
            if (string.IsNullOrEmpty(text))
                return text;

            return System.Text.RegularExpressions.Regex.Replace(text, "<[^>]*>", string.Empty);
        }


    }

This is intended to strip basic html tags, nothing too fancy. I am sure you can find cases where it doesn't work every time, so make sure you test for your situation.

Usage

To generate the intro text or short description, customize the property getter like so.  Again Cache or Persist the result as you see fit for better performance.

        public string ShortDescription {
            get {
                var intro= Description.StripHtml();

                if (intro.Length > 256)
                    return intro.Substring(0, 256);
                return intro;

            } }
Tags: c# extension method html strings

Copyright 2019 Cidean, LLC. All rights reserved.

Proudly running Umbraco 7. This site is responsive with the help of Foundation 5.