Compartir por


StringBuilder.CopyTo Método

Definición

Sobrecargas

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

Copia los caracteres de un segmento especificado de esta instancia en un intervalo de destino Char .

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

Copia los caracteres de un segmento especificado de esta instancia en un segmento especificado de una matriz de destino Char .

CopyTo(Int32, Span<Char>, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Copia los caracteres de un segmento especificado de esta instancia en un intervalo de destino Char .

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

Posición inicial de esta instancia en la que se copiarán los caracteres. El índice es de base cero.

destination
Span<Char>

Intervalo grabable donde se copiarán los caracteres.

count
Int32

Número de caracteres que se van a copiar.

Comentarios

El CopyTo método está pensado para usarse en la situación poco frecuente cuando es necesario copiar eficazmente secciones sucesivas de un StringBuilder objeto en un intervalo.

Por ejemplo, el código podría rellenar un StringBuilder objeto con un gran número de caracteres y, a continuación, usar el CopyTo método para copiar fragmentos pequeños y sucesivos del StringBuilder objeto en un intervalo en el que se procesan las piezas. Cuando se procesan todos los datos del StringBuilder objeto, el tamaño del StringBuilder objeto se establece en cero y se repite el ciclo.

Se aplica a

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

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Copia los caracteres de un segmento especificado de esta instancia en un segmento especificado de una matriz de destino Char .

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

Posición inicial de esta instancia en la que se copiarán los caracteres. El índice es de base cero.

destination
Char[]

Matriz donde se copiarán los caracteres.

destinationIndex
Int32

Posición inicial en la destination que se copiarán los caracteres. El índice es de base cero.

count
Int32

Número de caracteres que se van a copiar.

Atributos

Excepciones

destination es null.

sourceIndex, destinationIndexo count, es menor que cero.

O bien:

sourceIndex es mayor que la longitud de esta instancia.

sourceIndex + count es mayor que la longitud de esta instancia.

O bien:

destinationIndex + count es mayor que la longitud de destination.

Ejemplos

En el ejemplo siguiente se muestra el 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 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!

Comentarios

El CopyTo método está pensado para usarse en la situación poco frecuente cuando es necesario copiar eficazmente secciones sucesivas de un StringBuilder objeto en una matriz. La matriz debe ser un tamaño fijo, asignado previamente, reutilizable y posiblemente accesible globalmente.

Por ejemplo, el código podría rellenar un StringBuilder objeto con un gran número de caracteres y, a continuación, usar el CopyTo método para copiar fragmentos pequeños y sucesivos del StringBuilder objeto en una matriz donde se procesan las piezas. Cuando se procesan todos los datos del StringBuilder objeto, el tamaño del StringBuilder objeto se establece en cero y se repite el ciclo.

Se aplica a