Esempio n. 1
0
        //替换
        public bool Replace(StringLink2 match, StringLink2 replace, int index = 1)
        {
            if (match.IsEmpty() || replace.IsEmpty() || this.IsEmpty())
            {
                Console.WriteLine("串不能为空!");
                return(false);
            }

            if (index < 1 || index > this.GetLength())
            {
                Console.WriteLine("索引错误!");
                return(false);
            }

            int i = this.Index(match, index);

            if (i != 0)
            {
                this.Delete(i, match.GetLength());
                if (i > this.GetLength())
                {
                    return(this.Concat(replace));
                }
                else
                {
                    return(this.Insert(i, replace));
                }
            }

            return(false);
        }
Esempio n. 2
0
        //KMP匹配
        public int Index(StringLink2 match, int index = 1)
        {
            if (match.IsEmpty() || this.IsEmpty())
            {
                Console.WriteLine("串不能为空!");
                return(0);
            }

            if (index < 1 || index > this.GetLength())
            {
                Console.WriteLine("索引错误!");
                return(0);
            }

            int[] nextval = this._getNextVal(match);
            int   i       = index;
            int   j       = 1;

            while (i <= this.GetLength())
            {
                if (j == 0 || this[i] == match[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    j = nextval[j];
                }

                if (j > match.GetLength())
                {
                    return(i - match.GetLength());
                }
            }



            return(0);
        }