System.Text.StringBuilder.Remove Method

Removes the specified range of characters from this instance.

Syntax

public StringBuilder Remove (int startIndex, int length)

Parameters

startIndex
The zero-based position in this instance where removal begins.
length
The number of characters to remove.

Returns

A reference to this instance after the excise operation has completed.

Exceptions

TypeReason
ArgumentOutOfRangeException

startIndex or length is less than zero

-or-

The sum of startIndex and length is greater than the length of the current instance.

Remarks

The current method removes the specified range of characters from the current instance. The characters at (startIndex + length) are moved to startIndex, and the string value of the current instance is shortened by length. The capacity of the current instance is unaffected.

Note:

The StringBuilder.Remove(int, int) method modifies the value of the current System.Text.StringBuilder instance and returns that instance. It does not create and return a new System.Text.StringBuilder object.

Example

C# Example

using System;
using System.Text;

public class StringBuilderTest  {
   public static void Main()  {
   
      StringBuilder sb = new StringBuilder("0123456789");
      Console.WriteLine(sb);
      sb.Remove(3, 4);
      Console.WriteLine(sb);
   }
}
   

The output is

0123456789
012789

Requirements

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0