String.CopyTo(Int32, Char[], Int32, Int32) String.CopyTo(Int32, Char[], Int32, Int32) String.CopyTo(Int32, Char[], Int32, Int32) String.CopyTo(Int32, Char[], Int32, Int32) Method

Definición

Copia un número especificado de caracteres situados en una posición especificada de la instancia en una posición determinada de una matriz de caracteres Unicode.Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.

public:
 void CopyTo(int sourceIndex, cli::array <char> ^ destination, int destinationIndex, int count);
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)

Parámetros

sourceIndex
Int32 Int32 Int32 Int32

Índice del primer carácter de esta instancia que se va a copiar.The index of the first character in this instance to copy.

destination
Char[]

Matriz de caracteres Unicode a los que se copian los caracteres de esta instancia.An array of Unicode characters to which characters in this instance are copied.

destinationIndex
Int32 Int32 Int32 Int32

Índice de destination en el que comienza la operación de copia.The index in destination at which the copy operation begins.

count
Int32 Int32 Int32 Int32

Número de caracteres de la instancia en cuestión que se van a copiar en destination.The number of characters in this instance to copy to destination.

Excepciones

sourceIndex, destinationIndex o count es negativo.sourceIndex, destinationIndex, or count is negative O bien-or- sourceIndex no identifica una posición en la instancia actual.sourceIndex does not identify a position in the current instance.

O bien-or- destinationIndex no identifica un índice válido en la matriz destination.destinationIndex does not identify a valid index in the destination array.

o bien-or- count es mayor que la longitud de la subcadena desde sourceIndex hasta el final de esta instancia.count is greater than the length of the substring from sourceIndex to the end of this instance O bien-or- count es mayor que la longitud de la submatriz desde destinationIndex hasta el final de la matriz destination .count is greater than the length of the subarray from destinationIndex to the end of the destination array.

Ejemplos

En el siguiente ejemplo se CopyTo muestra el método.The following example demonstrates the CopyTo method.

using namespace System;
int main()
{
   
   // Embed an array of characters in a string
   String^ strSource = "changed";
   array<Char>^destination = {'T','h','e',' ','i','n','i','t','i','a','l',' ','a','r','r','a','y'};
   
   // Print the char array
   Console::WriteLine( destination );
   
   // Embed the source string in the destination string
   strSource->CopyTo( 0, destination, 4, strSource->Length );
   
   // Print the resulting array
   Console::WriteLine( destination );
   strSource = "A different string";
   
   // Embed only a section of the source string in the destination
   strSource->CopyTo( 2, destination, 3, 9 );
   
   // Print the resulting array
   Console::WriteLine( destination );
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray
using System;

public class CopyToTest {
    public static void Main() {

        // Embed an array of characters in a string
        string strSource = "changed";
    char [] destination = { 'T', 'h', 'e', ' ', 'i', 'n', 'i', 't', 'i', 'a', 'l', ' ',
                'a', 'r', 'r', 'a', 'y' };

        // Print the char array
        Console.WriteLine( destination );

        // Embed the source string in the destination string
        strSource.CopyTo ( 0, destination, 4, strSource.Length );

        // Print the resulting array
        Console.WriteLine( destination );

        strSource = "A different string";

        // Embed only a section of the source string in the destination
        strSource.CopyTo ( 2, destination, 3, 9 );

        // Print the resulting array
        Console.WriteLine( destination );
    }
}
// The example displays the following output:
//       The initial array
//       The changed array
//       Thedifferentarray
Public Class CopyToTest
    Public Shared Sub Main()
        ' Embed an array of characters in a string
        Dim strSource As String = "changed"
        Dim destination As Char() = {"T"c, "h"c, "e"c, " "c, "i"c, "n"c, "i"c, _
                    "t"c, "i"c, "a"c, "l"c, " "c, "a"c, "r"c, "r"c, "a"c, "y"c}

        ' Print the char array
        Console.WriteLine(destination)

        ' Embed the source string in the destination string
        strSource.CopyTo(0, destination, 4, strSource.Length)

        ' Print the resulting array
        Console.WriteLine(destination)

        strSource = "A different string"

        ' Embed only a section of the source string in the destination
        strSource.CopyTo(2, destination, 3, 9)

        ' Print the resulting array
        Console.WriteLine(destination)
    End Sub 
End Class 
' The example displays the following output:
'       The initial array
'       The changed array
'       Thedifferentarray

Comentarios

Este método copia count los caracteres de sourceIndex la posición de esta instancia en destinationIndex la posición destination de la matriz de caracteres.This method copies count characters from the sourceIndex position of this instance to the destinationIndex position of destination character array. Este método no cambia el tamaño de destination la matriz de caracteres; debe tener un número suficiente de elementos para alojar los caracteres copiados o el método ArgumentOutOfRangeExceptionproduce una excepción.This method does not resize the destination character array; it must have a sufficient number of elements to accommodate the copied characters or the method throws an ArgumentOutOfRangeException.

sourceIndexy destinationIndex son de base cero.sourceIndex and destinationIndex are zero-based.

Se aplica a

Consulte también: