StringBuilder.Remove Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Removes the specified range of characters from this instance.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

Syntax

Public Function Remove ( _
    startIndex As Integer, _
    length As Integer _
) As StringBuilder
public StringBuilder Remove(
    int startIndex,
    int length
)

Parameters

  • startIndex
    Type: System..::.Int32
    The zero-based position in this instance where removal begins.

Return Value

Type: System.Text..::.StringBuilder
A reference to this instance after the excise operation has completed.

Exceptions

Exception Condition
ArgumentOutOfRangeException

If startIndex or length is less than zero, or startIndex + length is greater than the length of this 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 Remove method modifies the value of the current StringBuilder instance and returns that instance. It does not create and return a new StringBuilder object.

Examples

The following code example demonstrates the Remove method.

Imports System.Text

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim rule1 As String = "0----+----1----+----2----+----3----+----4---"
      Dim rule2 As String = "01234567890123456789012345678901234567890123"
      Dim str As String = "The quick brown fox jumps over the lazy dog."
      Dim sb As New StringBuilder(str)

      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "StringBuilder.Remove method" & vbCrLf
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= "Original value:" & vbCrLf
      outputBlock.Text &= rule1 & vbCrLf
      outputBlock.Text &= rule2 & vbCrLf
      outputBlock.Text += String.Format("{0}", sb.ToString()) & vbCrLf
      outputBlock.Text &= vbCrLf

      sb.Remove(10, 6) ' Remove "brown "

      outputBlock.Text &= "New value:" & vbCrLf
      outputBlock.Text &= rule1 & vbCrLf
      outputBlock.Text &= rule2 & vbCrLf
      outputBlock.Text += String.Format("{0}", sb.ToString()) & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'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.
'
using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);

      outputBlock.Text += "\n";
      outputBlock.Text += "StringBuilder.Remove method" + "\n";
      outputBlock.Text += "\n";
      outputBlock.Text += "Original value:" + "\n";
      outputBlock.Text += rule1 + "\n";
      outputBlock.Text += rule2 + "\n";
      outputBlock.Text += String.Format("{0}", sb.ToString()) + "\n";
      outputBlock.Text += "\n";

      sb.Remove(10, 6); // Remove "brown "

      outputBlock.Text += "New value:" + "\n";
      outputBlock.Text += rule1 + "\n";
      outputBlock.Text += rule2 + "\n";
      outputBlock.Text += String.Format("{0}", sb.ToString()) + "\n";
   }
}
/*
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.

*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

StringBuilder Class

System.Text Namespace

Replace