/// <summary> /// 比较规则,先比较字符大小 /// 在比较长度 /// </summary> /// <param name="s"></param> /// <returns></returns> public int Compare(StringDS s) { int len = (this.GetLength() <= s.GetLength()) ? this.GetLength() : s.GetLength(); int i = 0; for (i = 0; i < len; i++) { if (this[i] != s[i]) { break; } }//for if (i < len) { if (this[i] < s[i]) { return -1; } else if (this[i] > s[i]) { return 1; } } else if (this.GetLength() == s.GetLength()) { return 0; } else if (this.GetLength() < s.GetLength()) { return -1; } return 1; }
/// <summary> /// 在索引后插入字符串 /// </summary> /// <param name="idx">从0开始</param> /// <param name="s"></param> /// <returns></returns> public StringDS Insert(int idx, StringDS s) { if (idx < 0 || idx > this.GetLength() - 1) { throw new IndexOutOfRangeException("索引超出范围。"); } int len = s.GetLength(); int len2 = this.GetLength() + len; StringDS s1 = new StringDS( len2); for (int i = 0; i <= idx; i++) { s1[i] = this[i]; }//for for (int i = idx + 1; i < idx +len ; i++) { s1[i] = s[i - idx-1]; }// for (int i = idx + len; i < len2; i++) { s1[i] = this[i - len+1]; } return s1; }
/// <summary> /// 连接串,将两个串合并成一个新串返回 /// </summary> /// <param name="s"></param> /// <returns></returns> public StringDS Concat(StringDS s) { StringDS s1 = new StringDS(this.GetLength() + s.GetLength()); for (int i = 0; i < this.GetLength(); i++) { s1[i] = this[i]; } for (int j = 0; j < s.GetLength(); j++) { s1[this.GetLength() + j] = s[j]; }// return s1; }