LearnFast.Ninja

Remove html tags from string using C# ASP.NET

In this tutorial we will use C# regular expression library (using System.Text.RegularExpression) to get rid of the html tags. This can be helpful when we want to use only the text and not display the style.

The function code:

public static string Remove_Html_Tags(string Html)

{

    string Only_Text = Regex.Replace(Html, @"<(.|\n)*?>"string.Empty);

 

    return Only_Text;

}


Example of how to use the function:

Description = "<p>We have sourced the best <b>travel deals</b> in U.K. Call us on <span class='7833'>1400 000 001</span>.</p>";

Description = HelpFunctions.Remove_Html_Tags(Description);


Result:


Tags

asp.net

remove-html-tags helpfunctions help-functions functions regular-expression regex replace