String.CopyTo Método

Definición

Sobrecargas

CopyTo(Span<Char>)

Copia el contenido de esta cadena en el intervalo de destino.

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

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.

CopyTo(Span<Char>)

Source:
String.cs
Source:
String.cs
Source:
String.cs

Copia el contenido de esta cadena en el intervalo de destino.

public:
 void CopyTo(Span<char> destination);
public void CopyTo (Span<char> destination);
member this.CopyTo : Span<char> -> unit
Public Sub CopyTo (destination As Span(Of Char))

Parámetros

destination
Span<Char>

Intervalo en el que se va a copiar el contenido de esta cadena.

Excepciones

El intervalo de destino es más corto que la cadena de origen.

Se aplica a

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

Source:
String.cs
Source:
String.cs
Source:
String.cs

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.

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

Índice del primer carácter de esta instancia que se va a copiar.

destination
Char[]

Matriz de caracteres Unicode a los que se copian los caracteres de esta instancia.

destinationIndex
Int32

Índice de destination en el que comienza la operación de copia.

count
Int32

Número de caracteres de la instancia en cuestión que se van a copiar en destination.

Excepciones

destination es null.

sourceIndex, destinationIndex o count es negativo.

O bien

sourceIndex no identifica una posición en la instancia actual.

O bien

destinationIndex no identifica un índice válido en la matriz destination.

O bien

count es mayor que la longitud de la subcadena desde sourceIndex hasta el final de esta instancia.

O bien

count es mayor que la longitud de la submatriz desde destinationIndex hasta el final de la matriz destination.

Ejemplos

En el siguiente ejemplo se muestra el CopyTo método.

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
// Embed an array of characters in a string
let strSource = "changed"
let destination = 
    [| 'T'; 'h'; 'e'; ' '; 'i'; 'n'; 'i'; 't'; 'i'; 'a'; 'l'; ' ';
       'a'; 'r'; 'r'; 'a'; 'y' |]

// Print the char array
printfn $"{destination}"

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

// Print the resulting array
printfn $"{destination}"

let strSource2 = "A different string"

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

// Print the resulting array
printfn $"{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 caracteres de la sourceIndex posición de esta instancia en la posición de la destinationIndex matriz de destination caracteres. Este método no cambia el tamaño de la destination matriz de caracteres; debe tener un número suficiente de elementos para acomodar los caracteres copiados o el método produce una ArgumentOutOfRangeExceptionexcepción .

sourceIndex y destinationIndex son de base cero.

Consulte también

Se aplica a