我正在使用 WebBrowser 控件处理 html 文档,我需要制作一个实用程序来搜索单词并在浏览器中突出显示它。如果字符串是英文的,它工作得很好,但对于其他语言的字符串,例如韩文,它似乎不起作用。
下面提到的代码工作的场景是 -
考虑用户在网页中选择了一个词“示例”,现在我需要突出显示这个词及其所有出现。我还需要计算它们的 byteOffset (代码片段只这样做)。
现在对于英语,下面的代码可以正常工作,但对于像韩语这样的语言,它根本不起作用。
它没有进入 for-each 循环
foreach (Match m in reg.Matches(this._documentContent))
这里 _documentContent 包含网页源作为字符串。没有发生。文档中所选单词的出现次数
这是代码, strTemp 包含韩文字符串:
string strTemp = myRange.text;
string strExp =@">(([^<])*?)" + strTemp + "(([^<])*?)<";
int intCount =0;
Regex reg = new Regex(strExp);
Regex reg1 = new Regex(strTemp);
foreach (Match m in reg.Matches(this._documentContent))
{
string strMatch = m.Value;
foreach (Match m2 in reg.Matches(strMatch))
{
intCount += 1;
if (intCount==OccurenceNo)
{
int intCharOffset = m.Index + m2.Index;
System.Text.UTF8Encoding d = new System.Text.UTF8Encoding();
int intByteOffset = d.GetBytes( _documentContent.Substring(1, intCharOffset)).Length;
}
}
}