CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Remove or Truncate the HTML Tags in a String Variable in C#
Submitted By Satheesh Babu B
On 3/5/2009 9:28:30 AM
Tags: C#,CodeDigest  

How to Remove or Truncate or Strip the HTML Tags in a String Variable in C#

Whenever we are capturing the user comments/inputs using rich textbox or any HTML editor, the captured text will be stored with the HTML tags.


In some places, we will require to truncate the text to a specified length. For example, to display the summary of bigger article we will display a part of the article in a index page and we will provide a link to the page that contains the entire article. In this scenario, before doing a substring operation we need to remove the HTML tags contained in the text to have an exact length of the text and to save the page design due to incomplete html tags. There will be many other scenarios where we required stripping out the html tags in a C# string.

 

This little code snippet will help us to remove the HTML tags contained in a string variable using Regular Expression.

 

protected void Page_Load(object sender, EventArgs e)
    {
        string str = "<b>I Love Dotnet</b>";
        Response.Write(RemoveHTMLTags(str));
    }
    public string RemoveHTMLTags(string source)
    {
        string expn = "<.*?>";
        return Regex.Replace(source, expn, string.Empty);
    }

 

Include System.Text.RegularExpressions namespace for the above code to work.

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..