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

Définition

Surcharges

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)

Copie les caractères d'un segment spécifié de cette instance vers un segment spécifié d'un tableau Char de destination.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)

Paramètres

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)

Copie les caractères d'un segment spécifié de cette instance vers un segment spécifié d'un tableau Char de destination.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)

Paramètres

sourceIndex
Int32 Int32 Int32 Int32

Position de départ dans cette instance à partir de laquelle les caractères sont copiés.The starting position in this instance where characters will be copied from. L'index est de base zéro.The index is zero-based.

destination
Char[]

Tableau vers lequel les caractères sont copiés.The array where characters will be copied.

destinationIndex
Int32 Int32 Int32 Int32

Position de départ dans destination où les caractères sont copiés.The starting position in destination where characters will be copied. L'index est de base zéro.The index is zero-based.

count
Int32 Int32 Int32 Int32

Nombre de caractères à copier.The number of characters to be copied.

Exceptions

sourceIndex, destinationIndex ou count est inférieur à zéro.sourceIndex, destinationIndex, or count, is less than zero.

- ou --or- sourceIndex est supérieur à la longueur de cette instance.sourceIndex is greater than the length of this instance.

sourceIndex + count est supérieur à la longueur de cette instance.sourceIndex + count is greater than the length of this instance.

ou-or- destinationIndex + count est supérieur à la longueur de destination.destinationIndex + count is greater than the length of destination.

Exemples

L’exemple suivant illustre la CopyTo méthode.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!

Remarques

La CopyTo méthode est destinée à être utilisée dans les rares cas où vous devez copier efficacement des sections consécutives d' StringBuilder un objet dans un tableau.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. Le tableau doit avoir une taille fixe, préallouée, réutilisable et éventuellement accessible globalement.The array should be a fixed size, preallocated, reusable, and possibly globally accessible.

Par exemple, votre application peut remplir un StringBuilder objet avec un grand nombre de caractères, puis utiliser CopyTo la méthode pour copier de petites parties successives StringBuilder de l’objet dans un tableau où les éléments sont traités.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. Lorsque toutes les données de l' StringBuilder objet sont traitées, la taille de l' StringBuilder objet est définie à zéro et le cycle est répété.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.

S’applique à