I have modified the appendAt so it will work if the index + length of new string is longer than the given stringbuilder
public void AppendAt(int index, StringBuilder s)
{
int len = this.StringBuilder.Length;
int reqcapacity = (index + s.Length + 1) - this.StringBuilder.Capacity;
if (reqcapacity > 0)
this.StringBuilder.Capacity += reqcapacity;
int initialLength = StringBuilder.Length;
//If we append near the end we can run out of space in the for loop. Make sure we are large enough
if (StringBuilder.Length < index + s.Length)
{
StringBuilder.Length = index + s.Length;
}
//If our appendAt is outside the scope we need to add spaces until then
if (index > initialLength-1)
{
for (int j = initialLength - 1; j < index; j++)
{
StringBuilder[j] = space;
}
}
for (int i = 0; i < s.Length; i++)
{
this.StringBuilder[i + index] = (char)(s[i]);
}
}