如何计算进入Web浏览器控件C#的字符数? [英] How do I count number of characters into webbrowser control C#?

查看:85
本文介绍了如何计算进入Web浏览器控件C#的字符数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Web浏览器控件。我想计算浏览器中的字符数,就像 Textbox 类的textchange一样。我只想计算显示 WebBrowser 没有html,没有图像等的文本中的字符数。有关如何模拟在更改文本时触发的文本框的行为的任何想法显示?谢谢

Using a webbrowser control. I'd like to count the number of characters into webbrowser, just like textchange of Textbox class. I just want to count the number of characters in the text that display WebBrowser no html, no images, etc.Any idea about how simulate the behavior of textbox which trigger when change text displayed? Thanks

我正在用C#开发Winforms。没有ASP.NET。

I'm developing Winforms in C#. No ASP.NET.

推荐答案

添加以下类:

using System.Text.RegularExpressions;

命名空间CK.TicketSystem.Shared
{
public static class HtmlUtils
{
public static bool IsHtmlFragment(string value)
{
return Regex.IsMatch(value,@);
}

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.

然后您这样做了:

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

希望有帮助

这篇关于如何计算进入Web浏览器控件C#的字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆