搜索模式C#的byte []

San*_*rst 0 c# search bytearray winforms

_documentContent包含整个文档作为html视图源. patternToFind包含要搜索的文本_documentContent.

如果语言是英语,下面的代码片段可以正常工作.但是当遇到像韩语这样的语言时,相同的代码根本不起作用.

样本文件

现在时

现在时,就像你学到的一样.你拿一个动词的字典形式,放下다,添加相应的结尾.

먹다 - 먹+어요=먹어요
마시다 - 마시+어요 - 마시어요 - 마셔요.

这个时态用于表示当前发生的事情.我吃.我喝.这是现在的总称.

当我试图找到먹时,下面的代码失败了.

有人可以建议一些解决方案

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Nol*_*rin 6

说真的,为什么不做以下呢?

var indexFound = documentContent.IndexOf("data");
Run Code Online (Sandbox Code Playgroud)

当原始数据是文本时,将字符串转换为字节数组然后搜索它们对我来说没有多大意义.如果愿意,您可以随时找到字节位置.