StringBuilder.CopyTo 方法

定義

多載

CopyTo(Int32, Span<Char>, Int32)

將此執行個體指定區段中的字元複製到目的地 Char 範圍。

CopyTo(Int32, Char[], Int32, Int32)

將此執行個體指定區段中的字元複製到目的端 Char 陣列的指定區段。

CopyTo(Int32, Span<Char>, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

將此執行個體指定區段中的字元複製到目的地 Char 範圍。

public:
 void CopyTo(int sourceIndex, Span<char> destination, int count);
public void CopyTo (int sourceIndex, Span<char> destination, int count);
member this.CopyTo : int * Span<char> * int -> unit
Public Sub CopyTo (sourceIndex As Integer, destination As Span(Of Char), count As Integer)

參數

sourceIndex
Int32

此執行個體中的開始位置,為字元的複製來源。 索引以零為起始。

destination
Span<Char>

可寫入範圍,將複製其中的字元。

count
Int32

要複製的字元數。

備註

CopyTo當您需要有效率地將物件的後續區段 StringBuilder 複製到範圍時,此方法適用于罕見的情況。

例如,您的程式碼可能會填入 StringBuilder 含有大量字元的物件,然後使用 CopyTo 方法將物件的小型連續片段 StringBuilder 複製到處理片段的範圍。 處理物件中的所有 StringBuilder 資料時,物件的大小 StringBuilder 會設定為零,而且會重複迴圈。

適用於

CopyTo(Int32, Char[], Int32, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

將此執行個體指定區段中的字元複製到目的端 Char 陣列的指定區段。

public:
 void CopyTo(int sourceIndex, cli::array <char> ^ destination, int destinationIndex, int count);
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
member this.CopyTo : int * char[] * int * int -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.CopyTo : int * char[] * int * int -> unit
Public Sub CopyTo (sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer)

參數

sourceIndex
Int32

此執行個體中的開始位置,為字元的複製來源。 索引以零為起始。

destination
Char[]

將複製其中字元的陣列。

destinationIndex
Int32

destination 中開始複製字元的位置。 索引以零為起始。

count
Int32

要複製的字元數。

屬性

例外狀況

destinationnull

sourceIndexdestinationIndexcount 小於零。

-或-

sourceIndex 大於這個執行個體的長度。

sourceIndex + count 大於這個執行個體的長度。

-或-

destinationIndex + count 大於 destination 的長度。

範例

下列範例示範 CopyTo 方法。

// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.
// Typically the destination array is small, preallocated, and global while 
// the StringBuilder is large with programmatically defined data. 
// However, for this example both the array and StringBuilder are small 
// and the StringBuilder has predefined data.

using namespace System;
using namespace System::Text;

int main()
{
   array<Char>^dest = gcnew array<Char>(6);
   StringBuilder^ src = gcnew StringBuilder( "abcdefghijklmnopqrstuvwxyz!" );
   dest[ 1 ] = ')';
   dest[ 2 ] = ' ';

   // Copy the source to the destination in 9 pieces, 3 characters per piece.
   Console::WriteLine( "\nPiece) Data:" );
   for ( int ix = 0; ix < 9; ix++ )
   {
      dest[ 0 ] = ix.ToString()[ 0 ];
      src->CopyTo( ix * 3, dest, 3, 3 );
      Console::Write( "    " );
      Console::WriteLine( dest );
   }
}

/*
This example produces the following results:

Piece) Data:
    0) abc
    1) def
    2) ghi
    3) jkl
    4) mno
    5) pqr
    6) stu
    7) vwx
    8) yz!
*/
// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.

// Typically the destination array is small, preallocated, and global while
// the StringBuilder is large with programmatically defined data.
// However, for this example both the array and StringBuilder are small
// and the StringBuilder has predefined data.

using System;
using System.Text;

class Sample
{
    protected static char[] dest = new char[6];
    public static void Main()
    {
    StringBuilder src = new StringBuilder("abcdefghijklmnopqrstuvwxyz!");
    dest[1] = ')';
    dest[2] = ' ';

// Copy the source to the destination in 9 pieces, 3 characters per piece.

    Console.WriteLine("\nPiece) Data:");
    for(int ix = 0; ix < 9; ix++)
        {
        dest[0] = ix.ToString()[0];
        src.CopyTo(ix * 3, dest, 3, 3);
        Console.Write("    ");
        Console.WriteLine(dest);
        }
    }
}
/*
This example produces the following results:

Piece) Data:
    0) abc
    1) def
    2) ghi
    3) jkl
    4) mno
    5) pqr
    6) stu
    7) vwx
    8) yz!
*/
// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.

// Typically the destination array is small, preallocated, and global while
// the StringBuilder is large with programmatically defined data.
// However, for this example both the array and StringBuilder are small
// and the StringBuilder has predefined data.

open System.Text

let dest = Array.zeroCreate 6

let src = StringBuilder "abcdefghijklmnopqrstuvwxyz!"
dest[1] <- ')'
dest[2] <- ' '

// Copy the source to the destination in 9 pieces, 3 characters per piece.

printfn "\Piece) Data:"
for i = 0 to 8 do
    dest[0] <- (string i)[0]
    src.CopyTo(i * 3, dest, 3, 3)
    printfn $"    {dest}"

// This example produces the following results:
//       Piece) Data:
//           0) abc
//           1) def
//           2) ghi
//           3) jkl
//           4) mno
//           5) pqr
//           6) stu
//           7) vwx
//           8) yz!
' Typically the destination array is small, preallocated, and global while 
' the StringBuilder is large with programmatically defined data. 
' However, for this example both the array and StringBuilder are small 
' and the StringBuilder has predefined data.

Imports System.Text

Class Sample
   Protected Shared dest(5) As Char
   
   Public Shared Sub Main()
      Dim src As New StringBuilder("abcdefghijklmnopqrstuvwxyz!")
      dest(1) = ")"c
      dest(2) = " "c
      
      ' Copy the source to the destination in 9 pieces, 3 characters per piece.
      Console.WriteLine(vbCrLf & "Piece) Data:")
      Dim ix As Integer
      For ix = 0 To 8
         dest(0) = ix.ToString()(0)
         src.CopyTo(ix * 3, dest, 3, 3)
         Console.Write("    ")
         Console.WriteLine(dest)
      Next ix
   End Sub
End Class
'
' This example produces the following results:
'
' Piece) Data:
'     0) abc
'     1) def
'     2) ghi
'     3) jkl
'     4) mno
'     5) pqr
'     6) stu
'     7) vwx
'     8) yz!

備註

CopyTo當您需要有效率地將物件的後續區段 StringBuilder 複製到陣列時,此方法適用于罕見的情況。 陣列應該是固定大小、預先配置、可重複使用,而且可能可全域存取。

例如,您的程式碼可能會填入 StringBuilder 含有大量字元的物件,然後使用 CopyTo 方法,將小型連續的物件片段 StringBuilder 複製到處理片段的陣列。 處理物件中的所有 StringBuilder 資料時,物件的大小 StringBuilder 會設定為零,而且會重複迴圈。

適用於