public StringBuilder Replace( string oldValue, string newValue, int startIndex, int count )
{
if (oldValue == null)
throw new ArgumentNullException ("The old value cannot be null.");
if (startIndex < 0 || count < 0 || startIndex > _length - count)
throw new ArgumentOutOfRangeException ();
if (oldValue.Length == 0)
throw new ArgumentException ("The old value cannot be zero length.");
string substr = _str.Substring(startIndex, count);
string replace = substr.Replace(oldValue, newValue);
// return early if no oldValue was found
if ((object) replace == (object) substr)
return this;
InternalEnsureCapacity (replace.Length + (_length - count));
// shift end part
if (replace.Length < count)
String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
else if (replace.Length > count)
String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
// copy middle part back into _str
String.CharCopy (_str, startIndex, replace, 0, replace.Length);
_length = replace.Length + (_length - count);
return this;
}