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

Definition

Überlädt

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)

Kopiert die Zeichen aus einem angegebenen Segment dieser Instanz in ein angegebenes Segment eines Ziel-Char-Arrays.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)

Parameter

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)

Kopiert die Zeichen aus einem angegebenen Segment dieser Instanz in ein angegebenes Segment eines Ziel-Char-Arrays.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)

Parameter

sourceIndex
Int32 Int32 Int32 Int32

Die Anfangsposition in dieser Instanz, ab der Zeichen kopiert werden.The starting position in this instance where characters will be copied from. Der Index ist nullbasiert.The index is zero-based.

destination
Char[]

Das Array, in das Zeichen kopiert werden.The array where characters will be copied.

destinationIndex
Int32 Int32 Int32 Int32

Die Anfangsposition in destination, an die Zeichen kopiert werden.The starting position in destination where characters will be copied. Der Index ist nullbasiert.The index is zero-based.

count
Int32 Int32 Int32 Int32

Die Anzahl der zu kopierenden Zeichen.The number of characters to be copied.

Ausnahmen

sourceIndex, destinationIndex oder count ist kleiner als 0 (null).sourceIndex, destinationIndex, or count, is less than zero.

- oder --or- sourceIndex ist größer als die Länge dieser Instanz.sourceIndex is greater than the length of this instance.

sourceIndex + count ist größer als die Länge dieser Instanz.sourceIndex + count is greater than the length of this instance.

- oder --or- destinationIndex + count ist größer als die Länge von destination.destinationIndex + count is greater than the length of destination.

Beispiele

Das folgende Beispiel veranschaulicht die CopyTo Methode.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
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!

Hinweise

Die CopyTo -Methode soll in seltenen Situationen verwendet werden, in denen Sie aufeinander folgende Abschnitte StringBuilder eines-Objekts effizient in ein Array kopieren müssen.The 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. Das Array muss eine festgelegte Größe, eine vorab Zuordnung, eine wiederverwendbare und möglicherweise Global zugänglich sein.The array should be a fixed size, preallocated, reusable, and possibly globally accessible.

Die Anwendung kann z. b. ein StringBuilder -Objekt mit einer großen Anzahl von Zeichen auffüllen und dann die CopyTo -Methode verwenden, um kleine StringBuilder , aufeinander folgende Teile des-Objekts in ein Array zu kopieren, in dem die Teile verarbeitet werden.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. Wenn alle Daten im StringBuilder -Objekt verarbeitet werden, wird die Größe StringBuilder des-Objekts auf 0 (null) festgelegt, und der Cycle wird wiederholt.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.

Gilt für: