StringBuilder.Remove(Int32, Int32) 方法

定义

将指定范围的字符从此实例中移除。

public:
 System::Text::StringBuilder ^ Remove(int startIndex, int length);
public System.Text.StringBuilder Remove (int startIndex, int length);
member this.Remove : int * int -> System.Text.StringBuilder
Public Function Remove (startIndex As Integer, length As Integer) As StringBuilder

参数

startIndex
Int32

此实例中开始移除操作的从零开始的位置。

length
Int32

要删除的字符数。

返回

切除操作完成后对此实例的引用。

例外

如果 startIndexlength 小于零,或者 startIndex + length 大于此实例的长度。

示例

下面的示例演示 Remove 方法。

using namespace System;
using namespace System::Text;
int main()
{
   String^ rule1 = "0----+----1----+----2----+----3----+----4---";
   String^ rule2 = "01234567890123456789012345678901234567890123";
   String^ str = "The quick brown fox jumps over the lazy dog.";
   StringBuilder^ sb = gcnew 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 );
   Console::WriteLine();
   sb->Remove( 10, 6 ); // Remove "brown "
   Console::WriteLine( "New value:" );
   Console::WriteLine( rule1 );
   Console::WriteLine( rule2 );
   Console::WriteLine( "{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.

*/
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.

*/
open System.Text

let rule1 = "0----+----1----+----2----+----3----+----4---"
let rule2 = "01234567890123456789012345678901234567890123"
let str = "The quick brown fox jumps over the lazy dog."
let sb = StringBuilder str

printfn "StringBuilder.Remove method\n"
printfn "Original value:"
printfn $"{rule1}"
printfn $"{rule2}"
printfn $"{sb}\n"

sb.Remove(10, 6) |> ignore // Remove "brown "

printfn "New value:"
printfn $"{rule1}"
printfn $"{rule2}"
printfn $"{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.
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
End Class
'
'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.
'

注解

当前方法从当前实例中删除指定的字符范围。 (startIndex + length) 处的字符将移动到 startIndex,并且当前实例的字符串值由 length缩短。 当前实例的容量不受影响。

注意

方法 Remove 修改当前 StringBuilder 实例的值并返回该实例。 它不会创建并返回新的 StringBuilder 对象。

适用于

另请参阅