首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >计数发生次数

计数发生次数
EN

Stack Overflow用户
提问于 2018-09-30 06:12:16
回答 2查看 95关注 0票数 -2

我正在编写这段代码,它可以在Web浏览器中找到来自列表的单词。在将这些单词替换为星号之前,我想计算出现的次数,并将其插入到数据库的另一列中。有什么解决方案吗?

下面是我的代码:

代码语言:javascript
复制
private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate(txbAdress.Text);
    webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    IHTMLDocument2 doc2 = webBrowser1.Document.DomDocument as IHTMLDocument2;
    StringBuilder html = new StringBuilder(doc2.body.outerHTML);


    string query;
    query = @"select Word from ListWords";

    List<string> words = new List<string>();

    DataSet ds;
    DataRow drow;

    ds = DatabaseConnection.Connection1(query);
    int index, total;

    total = ds.Tables[0].Rows.Count;

    string current_word;

    for (index = 0; index < total; index++ )
    {
        drow = ds.Tables[0].Rows[index];
        current_word = drow.ItemArray.GetValue(0).ToString();

        words.Add(current_word);
    }

    Console.WriteLine(query);


    Console.WriteLine("array:" + words);
    foreach (String key in words)
    {   
        int len = key.Length;
        string replace = "";

        for ( index = 0; index < len; index++)
        {
            replace += "*";
        }

        html.Replace(key, replace);
        //count++;
    }

    //Console.WriteLine("Total number of words: " + count);


    doc2.body.innerHTML = html.ToString();
}
EN

回答 2

Stack Overflow用户

发布于 2018-09-30 07:26:02

一种方法是使用正则表达式进行计数和替换:

代码语言:javascript
复制
Dictionary<string,int> counts = new Dictionary<string, int>();

foreach (var word in words)
{
    var matches = Regex.Matches(html, $@"\b{word}\b", RegexOptions.IgnoreCase);
    counts.Add(word, matches.Count);
    html = Regex.Replace(html, $@"\b{word}\b", "".PadLeft(word.Length, '*'), RegexOptions.IgnoreCase);
}

foreach (var kv in counts)
{
    Console.WriteLine($"{kv.Key}:{kv.Value}");
}
Console.WriteLine(html);
票数 -1
EN

Stack Overflow用户

发布于 2018-09-30 07:44:24

我希望这对你有帮助。

代码语言:javascript
复制
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace CountNumberTest
{
    public class WebBrowserReplaceHtmlFromDatabaseTest
    {
        private const string BaseAddress = "https://stackoverflow.com/questions/52572969/count-number-of-occurrence";

        private readonly ITestOutputHelper Output;
        private static readonly HttpClient StaticHttpComponent = new HttpClient
        {
            BaseAddress = new Uri(BaseAddress)
        };

        public WebBrowserReplaceHtmlFromDatabaseTest(ITestOutputHelper output)
        {
            this.Output = output;
        }

        [Fact]
        public async Task When_GetHtmlBodyFromWebPage_Then_ReplaceAllWordInHtml()
        {
            var replaceWordCollection = new[] { "html", "I", "in", "on" };
            var request = new HttpRequestMessage(HttpMethod.Get, $"{BaseAddress}");

            HttpResponseMessage response = await StaticHttpComponent.SendAsync(request);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            StringBuilder buffer = new StringBuilder(await response.Content.ReadAsStringAsync());

            Assert.NotEqual(0, buffer.Length);

            foreach (string word in replaceWordCollection)
            {
                int len = word.Length;

                int countOccurrence = Regex.Matches(buffer.ToString(), word).Count;
                var replaceWord = new string('*', len);

                Output.WriteLine($"{word} - occurred: {countOccurrence}");
                buffer.Replace(word, replaceWord);
            }

            string html = buffer.ToString();
            foreach (string word in replaceWordCollection)
                Assert.DoesNotContain(word, html);

            Output.WriteLine(html);
        }
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52572969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档