How do I count number of characters into webbrowser control C#?

≡放荡痞女 提交于 2019-12-01 14:50:42

Add the following class:

using System.Text.RegularExpressions;

namespace CK.TicketSystem.Shared { public static class HtmlUtils { public static bool IsHtmlFragment(string value) { return Regex.IsMatch(value, @""); }

    /// <summary>
    /// Remove tags from a html string
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string RemoveTags(string value)
    {
        if (value != null)
        {
            value = CleanHtmlComments(value);
            value = CleanHtmlBehaviour(value);
            value = Regex.Replace(value, @"</[^>]+?>", " ");
            value = Regex.Replace(value, @"<[^>]+?>", "");
            value = value.Trim();
        }
        return value;
    }

    /// <summary>
    /// Clean script and styles html tags and content
    /// </summary>
    /// <returns></returns>
    public static string CleanHtmlBehaviour(string value)
    {
        value = Regex.Replace(value, "(<style.+?</style>)|(<script.+?</script>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        return value;
    }

    /// <summary>
    /// Replace the html commens (also html ifs of msword).
    /// </summary>
    public static string CleanHtmlComments(string value)
    {
        //Remove disallowed html tags.
        value = Regex.Replace(value, "<!--.+?-->", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        return value;
    }

    /// <summary>
    /// Adds rel=nofollow to html anchors
    /// </summary>
    public static string HtmlLinkAddNoFollow(string value)
    {
        return Regex.Replace(value, "<a[^>]+href=\"?'?(?!#[\\w-]+)([^'\">]+)\"?'?[^>]*>(.*?)</a>", "<a href=\"$1\" rel=\"nofollow\" target=\"_blank\">$2</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    }
}

}

I must say that I found this class in some really good developer's blog, but unfortunately I can not remember where I did find it.

Then you do:

var str = HtmlUtils.RemoveTags(yourHtmlString);
var numberOfCharacters = str.Length;

Hope it helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!