Export (0) Print
Expand All

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
Not applicable.

Parameters

startIndex

The zero-based 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 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 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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft