Remove Method
Collapse the table of content
Expand the table of content

StringBuilder.Remove Method

Removes the specified range of characters from this instance.

[Visual Basic]
Public Function Remove( _
   ByVal startIndex As Integer, _
   ByVal length As Integer _
) As StringBuilder
[C#]
public StringBuilder Remove(
 int startIndex,
 int length
);
[C++]
public: StringBuilder* Remove(
 int startIndex,
 int length
);
[JScript]
public function Remove(
   startIndex : int,
 length : int
) : StringBuilder;

Parameters

startIndex
The position where removal begins.
length
The number of characters to remove.

Return Value

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

Exceptions

Exception Type 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 is unaffected.

Example

[Visual Basic] 
' This example demonstrates StringBuilder.Remove()
Imports System
Imports System.Text

Class Sample
   Public Shared Sub Main()
      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)
      
      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())
   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.
'

[C#] 
// This example demonstrates StringBuilder.Remove()
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.

*/

[C++] 
// This example demonstrates StringBuilder.Remove()
#using <mscorlib.dll>
using namespace System;
using namespace System::Text;

int main() 
{
    String* rule1 = S"0----+----1----+----2----+----3----+----4---";
    String* rule2 = S"01234567890123456789012345678901234567890123";
    String* str =   S"The quick brown fox jumps over the lazy dog.";
    StringBuilder* sb = new StringBuilder(str);

    Console::WriteLine();
    Console::WriteLine(S"StringBuilder.Remove method");
    Console::WriteLine();
    Console::WriteLine(S"Original value:");
    Console::WriteLine(rule1);
    Console::WriteLine(rule2);
    Console::WriteLine(S"{0}",sb);
    Console::WriteLine();

    sb->Remove(10, 6); // Remove "brown "

    Console::WriteLine(S"New value:");
    Console::WriteLine(rule1);
    Console::WriteLine(rule2);
    Console::WriteLine(S"{0}",sb);
}
/*
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.

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

StringBuilder Class | StringBuilder Members | System.Text Namespace | Replace

Show:
© 2016 Microsoft