//替换字符串 public int Replace(StringLink match, StringLink replace, int index = 1) { if (match.IsEmpty() || this.IsEmpty()) { Console.WriteLine("当前串或匹配串不能为空!"); return(0); } if (index < 1 || index > this.GetLength()) { Console.WriteLine("索引错误!"); return(0); } int matchIndex = this.Index(match, index); if (matchIndex != 0) { this.Delete(matchIndex, match.GetLength()); if (matchIndex - 1 == this.GetLength()) { this.Concat(replace); } else { this.Insert(matchIndex, replace); } return(1); } return(0); }
//KMP匹配串的位置 public int Index(StringLink s, int where = 1) { if (s.IsEmpty() || this.IsEmpty() || where < 1 || where > this.GetLength()) { Console.WriteLine("串不能为空或索引非法!"); return(0); } int[] nextval = this._GetNextVal(s); int matchLength = s.GetLength(); int length = this.GetLength(); int i = where; int j = 1; while (i <= length && j <= matchLength) { if (j == 0 || this[i] == s[j]) { j++; i++; } else { j = nextval[j]; } } if (j > matchLength) { return(i - matchLength); } return(0); }
//在尾部链接串 public bool Concat(StringLink s) { if (s.IsEmpty()) { Console.WriteLine("链接的串不能为空!"); return(false); } char[] chars = s.ToChars(); return(this.Concat(chars)); }
//插入字符串到指定位置 public bool Insert(int index, StringLink s) { if (s.IsEmpty()) { Console.WriteLine("插入的字符串不能为空!"); return(false); } char[] chars = s.ToChars(); return(this.Insert(index, chars)); }