StringBuilder.CopyTo Metodo

Definizione

Overload

CopyTo(Int32, Span<Char>, Int32)

Copia i caratteri da un segmento specificato di questa istanza a un intervallo Char di destinazione.

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

Copia i caratteri da un segmento specificato di questa istanza a un segmento specificato di una matrice Char di destinazione.

CopyTo(Int32, Span<Char>, Int32)

Origine:
StringBuilder.cs
Origine:
StringBuilder.cs
Origine:
StringBuilder.cs

Copia i caratteri da un segmento specificato di questa istanza a un intervallo Char di destinazione.

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)

Parametri

sourceIndex
Int32

Posizione iniziale in questa istanza da cui verranno copiati i caratteri. L'indice è in base zero.

destination
Span<Char>

Intervallo scrivibile in cui verranno copiati i caratteri.

count
Int32

Numero dei caratteri da copiare.

Commenti

Il CopyTo metodo deve essere usato nella rara situazione quando è necessario copiare in modo efficiente le sezioni successive di un StringBuilder oggetto in un intervallo.

Ad esempio, il codice potrebbe popolare un StringBuilder oggetto con un numero elevato di caratteri, quindi usare il CopyTo metodo per copiare piccoli pezzi successivi dell'oggetto StringBuilder in un intervallo in cui vengono elaborati i pezzi. Quando tutti i dati nell'oggetto vengono elaborati, le dimensioni dell'oggetto StringBuilderStringBuilder vengono impostate su zero e il ciclo viene ripetuto.

Si applica a

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

Origine:
StringBuilder.cs
Origine:
StringBuilder.cs
Origine:
StringBuilder.cs

Copia i caratteri da un segmento specificato di questa istanza a un segmento specificato di una matrice Char di destinazione.

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)

Parametri

sourceIndex
Int32

Posizione iniziale in questa istanza da cui verranno copiati i caratteri. L'indice è in base zero.

destination
Char[]

Matrice in cui verranno copiati i caratteri.

destinationIndex
Int32

Posizione iniziale in destination in cui verranno copiati i caratteri. L'indice è in base zero.

count
Int32

Numero dei caratteri da copiare.

Attributi

Eccezioni

destination è null.

sourceIndex, destinationIndex o count è minore di zero.

-oppure-

sourceIndex è maggiore della lunghezza di questa istanza.

sourceIndex + count è maggiore della lunghezza di questa istanza.

-oppure-

destinationIndex + count è maggiore della lunghezza di destination.

Esempio

Nell'esempio seguente viene illustrato il CopyTo metodo.

// 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!

Commenti

Il CopyTo metodo deve essere usato nella situazione rara quando è necessario copiare in modo efficiente le sezioni successive di un StringBuilder oggetto in una matrice. La matrice deve essere una dimensione fissa, preallocata, riutilizzabile e possibilmente accessibile a livello globale.

Ad esempio, il codice potrebbe popolare un StringBuilder oggetto con un numero elevato di caratteri e quindi usare il CopyTo metodo per copiare piccole parti successive dell'oggetto StringBuilder in una matrice in cui vengono elaborati i pezzi. Quando tutti i dati nell'oggetto vengono elaborati, le dimensioni dell'oggetto StringBuilderStringBuilder vengono impostate su zero e il ciclo viene ripetuto.

Si applica a