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

后端 未结 1 829
粉色の甜心
粉色の甜心 2021-01-17 05:53

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

1条回答
  •  再見小時候
    2021-01-17 06:41

    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, @""); }

        /// 
        /// Remove tags from a html string
        /// 
        /// 
        /// 
        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;
        }
    
        /// 
        /// Clean script and styles html tags and content
        /// 
        /// 
        public static string CleanHtmlBehaviour(string value)
        {
            value = Regex.Replace(value, "()|()", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    
            return value;
        }
    
        /// 
        /// Replace the html commens (also html ifs of msword).
        /// 
        public static string CleanHtmlComments(string value)
        {
            //Remove disallowed html tags.
            value = Regex.Replace(value, "", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    
            return value;
        }
    
        /// 
        /// Adds rel=nofollow to html anchors
        /// 
        public static string HtmlLinkAddNoFollow(string value)
        {
            return Regex.Replace(value, "]+href=\"?'?(?!#[\\w-]+)([^'\">]+)\"?'?[^>]*>(.*?)", "$2", 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

    0 讨论(0)
提交回复
热议问题