【发布时间】:2010-12-15 10:32:03
【问题描述】:
_documentContent 包含整个文档作为 html 视图源。
patternToFind 包含要在 _documentContent 中搜索的文本。
如果语言是英语,下面的代码 sn-p 可以正常工作。 但是,当遇到像韩语这样的语言时,相同的代码根本不起作用。
样本文件现在时
现在时与你所学的一样。您采用动词的字典形式,去掉다,添加适当的结尾。
먹다 - 먹 + 어요 = 먹어요
마시다 - 마시 + 어요 - 마시어요 - 마셔요。
这个时态用来表示现在发生的事情。我吃。我喝。它是现在的总称。
当我试图找到 먹 时,下面的代码会失败。
有人可以提出一些解决方案
using System;
using System.Collections.Generic;
using System.Text;
namespace MultiByteStringHandling
{
class Program
{
static void Main(string[] args)
{
string _documentContent = @"먹다 - 먹 + 어요 = 먹어요";
byte[] patternToFind = Encoding.UTF8.GetBytes("먹");
byte[] DocumentBytes = Encoding.UTF8.GetBytes(_documentContent);
int intByteOffset = indexOf(DocumentBytes, patternToFind);
Console.WriteLine(intByteOffset.ToString());
}
public int indexOf(byte[] data, byte[] pattern)
{
int[] failure = computeFailure(pattern);
int j = 0;
if (data.Length == 0) return 0;
for (int i = 0; i < data.Length; i++)
{
while (j > 0 && pattern[j] != data[i])
{
j = failure[j - 1];
}
if (pattern[j] == data[i])
{
j++;
}
if (j == pattern.Length)
{
return i - pattern.Length + 1;
}
}
return -1;
}
/**
* Computes the failure function using a boot-strapping process,
* where the pattern is matched against itself.
*/
private int[] computeFailure(byte[] pattern)
{
int[] failure = new int[pattern.Length];
int j = 0;
for (int i = 1; i < pattern.Length; i++)
{
while (j > 0 && pattern[j] != pattern[i])
{
j = failure[j - 1];
}
if (pattern[j] == pattern[i])
{
j++;
}
failure[i] = j;
}
return failure;
}
}
}
【问题讨论】:
-
请使用失败的示例文档/模式更新代码。我将进行编辑以从问题中删除对 WinForms 的引用,因为它显然与 WinForms 本身没有任何关系。
-
在处理带有韩文字符的内容时,是否可以手动查看 byte[] 数据中的 byte[] 模式?输入文件真的是 UTF-8 还是 ANSI 代码页或类似文件?
-
有什么特别的原因要将字符串转换为字节数组,而不是仅仅做
_documentContent.IndexOf("data")? -
我有一份韩文文档,我需要在文档中搜索韩文文本。该文件确实包含英文的某些部分。这就是为什么我尝试使用字节数组来做到这一点。
-
.NET 中的字符串在内存中是 unicode,所以它应该能够搜索韩文文本就好了。您是否尝试过使用 .IndexOf?
标签: c# winforms search bytearray