StringBuilder.CopyTo Método

Definição

Sobrecargas

CopyTo(Int32, Span<Char>, Int32)

Copia os caracteres de um segmento especificado desta instância para um intervalo Char de destino.

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

Copia os caracteres de um segmento especificado desta instância para um segmento especificado de uma matriz Char de destino.

CopyTo(Int32, Span<Char>, Int32)

Origem:
StringBuilder.cs
Origem:
StringBuilder.cs
Origem:
StringBuilder.cs

Copia os caracteres de um segmento especificado desta instância para um intervalo Char de destino.

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)

Parâmetros

sourceIndex
Int32

A posição inicial nessa instância de onde os caracteres serão copiados. O índice é baseado em zero.

destination
Span<Char>

O intervalo gravável em que os caracteres serão copiados.

count
Int32

O número de caracteres a serem copiados.

Comentários

O CopyTo método destina-se a ser usado na rara situação em que você precisa copiar com eficiência seções sucessivas de um StringBuilder objeto para um intervalo.

Por exemplo, seu código pode preencher um StringBuilder objeto com um grande número de caracteres e, em seguida, usar o CopyTo método para copiar partes pequenas e sucessivas do StringBuilder objeto para um intervalo em que as partes são processadas. Quando todos os dados no StringBuilder objeto são processados, o tamanho do StringBuilder objeto é definido como zero e o ciclo é repetido.

Aplica-se a

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

Origem:
StringBuilder.cs
Origem:
StringBuilder.cs
Origem:
StringBuilder.cs

Copia os caracteres de um segmento especificado desta instância para um segmento especificado de uma matriz Char de destino.

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)

Parâmetros

sourceIndex
Int32

A posição inicial nessa instância de onde os caracteres serão copiados. O índice é baseado em zero.

destination
Char[]

A matriz na qual os caracteres serão copiados.

destinationIndex
Int32

A posição inicial no destination na qual os caracteres serão copiados. O índice é baseado em zero.

count
Int32

O número de caracteres a serem copiados.

Atributos

Exceções

destination é null.

sourceIndex, destinationIndex ou count é menor que zero.

- ou -

sourceIndex é maior que o tamanho desta instância.

sourceIndex + count é maior que o tamanho desta instância.

- ou -

destinationIndex + count é maior que o comprimento do destination.

Exemplos

O exemplo a seguir demonstra o CopyTo método.

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

Comentários

O CopyTo método destina-se a ser usado na rara situação em que você precisa copiar com eficiência seções sucessivas de um StringBuilder objeto para uma matriz. A matriz deve ser um tamanho fixo, pré-alocado, reutilizável e possivelmente acessível globalmente.

Por exemplo, seu código pode preencher um StringBuilder objeto com um grande número de caracteres e, em seguida, usar o CopyTo método para copiar partes pequenas e sucessivas do StringBuilder objeto para uma matriz em que as partes são processadas. Quando todos os dados no StringBuilder objeto são processados, o tamanho do StringBuilder objeto é definido como zero e o ciclo é repetido.

Aplica-se a