C# 按单词而不是字符计数字符串
我正在编写这段代码,它可以从Web浏览器中识别下面列出的单词。一旦识别出这些单词,它们将变成星号,并将计算有多少单词被替换,但它不起作用。有人能帮忙吗 这是我的密码:C# 按单词而不是字符计数字符串,c#,string,count,webbrowser-control,C#,String,Count,Webbrowser Control,我正在编写这段代码,它可以从Web浏览器中识别下面列出的单词。一旦识别出这些单词,它们将变成星号,并将计算有多少单词被替换,但它不起作用。有人能帮忙吗 这是我的密码: private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate(txbAdress.Text); webBrowser1.DocumentCompleted += webBrowser1_Doc
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 = @"
从列表单词中选择单词
“
List words=new List();
数据集ds;
数据行卓尔;
ds=DatabaseConnection.Connection1(查询);
int指数,总计;
总计=ds.Tables[0].Rows.Count;
字符串当前_字;
对于(索引=0;索引
更换前只需清点一下就可以了
给定的
public int CountOccurrences(string source, string match)
{
var pos = 0;
var count = 0;
while ((pos < source.Length) && (pos = source.IndexOf(match, pos, StringComparison.Ordinal)) != -1)
{
count++;
pos += match.Length;
}
return count;
}
“但它不起作用”-你能解释一下怎么做吗?你想知道
单词中有多少单词被匹配,或者有多少单词被替换了吗?是“abc bobo bobo bobo测试”3个单词还是1个单词?gagaotest呢?有多少单词被替换了“abc bobo bobo bobo测试”3 boboHi kyte,请用附加细节编辑你的问题(描述它是如何工作的->输入是什么,预期输出是什么,实际输出是什么)而不是将这些细节放在评论中。约翰,没有任何文字会被计算,因为没有“gago”
public int CountOccurrences(string source, string match)
{
var pos = 0;
var count = 0;
while ((pos < source.Length) && (pos = source.IndexOf(match, pos, StringComparison.Ordinal)) != -1)
{
count++;
pos += match.Length;
}
return count;
}
var count = CountOccurrences(html,key);
html.Replace(key, substitution);