StringBuilder.Remove Method

Removes the specified range of characters from this instance.

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

public StringBuilder Remove (
	int startIndex,
	int length
)
public StringBuilder Remove (
	int startIndex, 
	int length
)
public function Remove (
	startIndex : int, 
	length : int
) : StringBuilder

Parameters

startIndex

The position in this instance where removal begins.

length

The number of characters to remove.

Return Value

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

Exception typeCondition

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 current instance is unaffected.

The following code 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.

*/

// This example demonstrates StringBuilder.Remove()
import System.*;
import System.Text.*;

class Sample
{
    public static void main(String[] args)
    {
        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());
    } //main
} //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.

*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: