Solved: C# How Get Plain Text from HTML String

 

At the last time I’ve found perfect function which will help to remove all HTML tags, scripts, css, styles from html string and convert it to a plain text.

using System.Text.RegularExpressions;
private string GetPlainTextFromHtml(string htmlString)
{
    string htmlTagPattern = "<.*?>";
    var regexCss = new Regex("(\\<script(.+?)\\)|(\\<style(.+?)\\)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
    htmlString = regexCss.Replace(htmlString, string.Empty);
    htmlString = Regex.Replace(htmlString, htmlTagPattern, string.Empty);
    htmlString = Regex.Replace(htmlString, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
    htmlString = htmlString.Replace(" ", string.Empty);

    return htmlString;
}

Presented solution comes from source and Works Perfect!!! Very useful function 🙂

March 6th, 2018