Freigeben über


StringBuilder.Remove Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Removes the specified range of characters from this instance.

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

Syntax

'Declaration
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.
  • length
    Type: System.Int32
    The number of characters to remove.

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.

NoteNote:

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

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.