Posts Tagged ‘Html’
09
Oct

Strip HTML Tags

1 Comment » Popularity: 3%

Corey referred a very useful code snippet provided by John which helps you to strip HTML Tags.

Strip HTML Tags

View Demo: New Window
csharp title:
private string StripHtmlTags(string value){
    int length = 0;
    int.TryParse(value, out length);

    // Remove HTML tags and empty newlines and spaces and leading spaces
    string formattedValue = Regex.Replace(value as string, "<.*?>", "");
    formattedValue = Regex.Replace(formattedValue, @"\n+\s+", "\n\n");
    formattedValue = formattedValue.TrimStart(' ');
    formattedValue = HttpUtility.HtmlDecode(formattedValue);
    if(length > 0 && formattedValue.Length >= length)
        formattedValue = formattedValue.Substring(0, length - 1);
    return formattedValue;
}