EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

StringBuilder.Remove Method

May 11, 2014

Removes the specified range of characters from this instance.

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

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.

ExceptionCondition
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.

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.

The following code example demonstrates the Remove method.


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.

*/


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft