StringBuilder.Remove Method
Removes the specified range of characters from this instance.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- startIndex
- Type: System.Int32
The zero-based position in this instance where removal begins.
- length
- Type: System.Int32
The number of characters to remove.
Return Value
Type: System.Text.StringBuilderA reference to this instance after the excise operation has completed.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | If startIndex or length is less than zero, or startIndex + length is greater than the length of this instance. |
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 Remove method modifies the value of the current StringBuilder instance and returns that instance. It does not create and return a new StringBuilder object. |
The following example demonstrates the Remove method.
using System; using System.Text; class Sample { public static void Main() { string rule1 = "0----+----1----+----2----+----3----+----4---"; string rule2 = "01234567890123456789012345678901234567890123"; string str = "The quick brown fox jumps over the lazy dog."; StringBuilder sb = new StringBuilder(str); Console.WriteLine(); Console.WriteLine("StringBuilder.Remove method"); Console.WriteLine(); Console.WriteLine("Original value:"); Console.WriteLine(rule1); Console.WriteLine(rule2); Console.WriteLine("{0}", sb.ToString()); Console.WriteLine(); sb.Remove(10, 6); // Remove "brown " Console.WriteLine("New value:"); Console.WriteLine(rule1); Console.WriteLine(rule2); Console.WriteLine("{0}", sb.ToString()); } } /* This example produces the following results: StringBuilder.Remove method Original value: 0----+----1----+----2----+----3----+----4--- 01234567890123456789012345678901234567890123 The quick brown fox jumps over the lazy dog. New value: 0----+----1----+----2----+----3----+----4--- 01234567890123456789012345678901234567890123 The quick fox jumps over the lazy dog. */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note