共用方式為


StringBuilder.CopyTo 方法

定義

多載

名稱 Description
CopyTo(Int32, Span<Char>, Int32)

將該實例指定區段的字元複製到目的 Char 區間。

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

將該實例指定區段的字元複製到目標 Char 陣列的指定區段。

CopyTo(Int32, Span<Char>, Int32)

來源:
StringBuilder.cs
來源:
StringBuilder.cs
來源:
StringBuilder.cs
來源:
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)

來源:
StringBuilder.cs
來源:
StringBuilder.cs
來源:
StringBuilder.cs
來源:
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

sourceIndexdestinationIndex, 或 count小於零。

-或-

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 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 會被設為零,並重複這個循環。

適用於