//删除指定位置长度的字符串 public bool Delete(int index, int length) { if (this.isEmpty()) { Console.WriteLine("被删除的字符串为空!"); return false; } int sounceLength = this._data.Length; if (index < 1 || index > sounceLength || length < 1 || index + length - 1 > sounceLength) { Console.WriteLine("索引位置和长度错误!"); return false; } char[] term = this._data; int newLength = sounceLength - length; this._data = new char[newLength]; int go = 0; for (int i = 0; i < newLength; i++) { if (i == index - 1) { go = length; } this._data[i] = term[i + go]; } return true; }
①同样的需要判断串是否为空和索引的合法性
②临时保存原串的数据
③重构数据域大小为删除字符串后的长度
④遍历赋值,通过go位移跳过被删除的元素