System.StringHelper.Match C# (CSharp) Method

Match() public static method

模糊匹配
public static Match ( String str, String key, Int32 maxError ) : Int32>.KeyValuePair
str String
key String
maxError Int32
return Int32>.KeyValuePair
        public static KeyValuePair<Int32, Int32> Match(String str, String key, Int32 maxError = 0)
        {
            /*
             * 字符串 abcdef
             * 少字符 ace      (3, 0)
             * 多字符 abkcd    (4, 1)
             * 改字符 abmd     (3, 1)
             */

            // str下一次要匹配的位置
            var m = 0;
            // key下一次要匹配的位置
            var k = 0;

            // 总匹配数
            var match = 0;
            // 跳过次数
            var skip = 0;

            while (skip <= maxError && k < key.Length)
            {
                // 向前逐个匹配
                for (var i = m; i < str.Length; i++)
                {
                    if (str[i] == key[k])
                    {
                        k++;
                        m = i + 1;
                        match++;

                        // 如果已完全匹配,则结束
                        if (k == key.Length) break;
                    }
                }

                // 如果已完全匹配,则结束
                if (k == key.Length) break;

                // 没有完全匹配,跳过关键字中的一个字符串,从上一次匹配后面继续找
                k++;
                skip++;
            }

            return new KeyValuePair<Int32, Int32>(match, skip);
        }

Same methods

StringHelper::Match ( IEnumerable list, String keys, Func keySelector ) : Double>>.IEnumerable
StringHelper::Match ( IEnumerable list, String keys, Func keySelector, Int32 count, Double confidence = 0.5 ) : IEnumerable