StringBuilder.CopyTo StringBuilder.CopyTo StringBuilder.CopyTo StringBuilder.CopyTo Method

定义

重载

CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32)
CopyTo(Int32, Char[], Int32, Int32) CopyTo(Int32, Char[], Int32, Int32) CopyTo(Int32, Char[], Int32, Int32) CopyTo(Int32, Char[], Int32, Int32)

将此实例的指定段中的字符复制到目标 Char 数组的指定段中。Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32) CopyTo(Int32, Span<Char>, Int32)

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 Int32 Int32 Int32
destination
Span<Char>

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

将此实例的指定段中的字符复制到目标 Char 数组的指定段中。Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

public:
 void CopyTo(int sourceIndex, cli::array <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
Public Sub CopyTo (sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer)

参数

sourceIndex
Int32 Int32 Int32 Int32

此实例中开始复制字符的位置。The starting position in this instance where characters will be copied from. 索引是从零开始的。The index is zero-based.

destination
Char[]

将从中复制字符的数组。The array where characters will be copied.

destinationIndex
Int32 Int32 Int32 Int32

destination 中将从其开始复制字符的起始位置。The starting position in destination where characters will be copied. 索引是从零开始的。The index is zero-based.

count
Int32 Int32 Int32 Int32

要复制的字符数。The number of characters to be copied.

异常

sourceIndexdestinationIndexcount 小于零。sourceIndex, destinationIndex, or count, is less than zero.

- 或 --or- sourceIndex 大于此实例的长度。sourceIndex is greater than the length of this instance.

sourceIndex + count 大于此实例的长度。sourceIndex + count is greater than the length of this instance.

- 或 --or- destinationIndex + count 大于 destination 的长度。destinationIndex + count is greater than the length of destination.

示例

下面的示例演示CopyTo方法。The following example demonstrates the CopyTo method.

// 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!
*/
' 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 'Main
End Class 'Sample
'
' 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!

注解

当你需要将StringBuilder对象的连续部分有效地复制到数组时, 此方法旨在用于极少数情况。CopyToThe CopyTo method is intended to be used in the rare situation when you need to efficiently copy successive sections of a StringBuilder object to an array. 该数组应为固定大小、预分配、可重用并且可能可全局访问。The array should be a fixed size, preallocated, reusable, and possibly globally accessible.

例如, 你的应用程序可以StringBuilder CopyTo使用大量字符来填充对象, 然后使用方法将StringBuilder对象的小型连续部分复制到处理这些部分的数组。For example, your application could populate a StringBuilder object with a large number of characters then use the CopyTo method to copy small, successive pieces of the StringBuilder object to an array where the pieces are processed. 在处理StringBuilder对象中的所有数据时, StringBuilder对象的大小设置为零, 并重复周期。When all the data in the StringBuilder object is processed, the size of the StringBuilder object is set to zero and the cycle is repeated.

适用于