我正在使用WebBrowser控件处理html文档,我需要制作一个实用程序来搜索单词并在浏览器中突出显示它。如果字符串是英文的,它的效果很好,但对于其他语言的字符串,例如韩文,它似乎不起作用。使用正则表达式搜索MultiByte字符串
在下文提到的代码情景的作品是 -
考虑用户在网页中选择了一个词“示例”,现在我需要强调这个词和它的所有出现。此外,我需要计算他们的byteOffset(代码片段只)。
现在英语语言下面的代码工作正常,但对于像韩语这样的语言,它根本不起作用。
它不是for-each循环
foreach (Match m in reg.Matches(this._documentContent))
内到达这里_documentContent包含网页源的字符串。 发生否是否。文档
在这里,在选字的occurence的是代码,strTemp包含韩国字符串:
string strTemp = myRange.text;
string strExp [email protected]">(([^<])*?)" + 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;
}
}
}
显示实际的代码可能会帮助... – RageZ 2009-11-10 08:33:06
你可以发布一些代码。 C#是unicode,所以只要你的编码被正确读取,你应该能够对字符串做任何事情。 – 2009-11-10 08:33:29