String.CopyTo Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Sobrecargas
| Name | Description |
|---|---|
| CopyTo(Span<Char>) |
Copia o conteúdo desta cadeia para o intervalo de destino. |
| CopyTo(Int32, Char[], Int32, Int32) |
Copia um número especificado de caracteres de uma posição especificada neste caso para uma posição especificada num array de caracteres Unicode. |
CopyTo(Span<Char>)
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
Copia o conteúdo desta cadeia para o 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
Exceções
O intervalo de destino é mais curto do que a cadeia de origem.
Aplica-se a
CopyTo(Int32, Char[], Int32, Int32)
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
- Origem:
- String.cs
Copia um número especificado de caracteres de uma posição especificada neste caso para uma posição especificada num array 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
O índice do primeiro carácter neste caso a copiar.
- destination
- Char[]
Um array de caracteres Unicode para os quais os caracteres neste caso são copiados.
- destinationIndex
- Int32
O índice em destination onde começa a operação de cópia.
- count
- Int32
O número de caracteres neste caso a copiar para destination.
Exceções
destination é null.
sourceIndex, destinationIndex, ou count é negativo
-ou-
sourceIndex não identifica uma posição na instância atual.
-ou-
destinationIndex não identifica um índice válido no destination array.
-ou-
count é maior do que o comprimento da subcorda de sourceIndex até ao fim desta instância
-ou-
count é maior do que o comprimento do subarray de destinationIndex até ao fim do destination array.
Exemplos
O exemplo seguinte demonstra o CopyTo método.
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
Observações
Este método copia count caracteres da sourceIndex posição desta instância para a destinationIndex posição do destination array de caracteres. Este método não redimensiona o array de destination caracteres; deve ter um número suficiente de elementos para acomodar os caracteres copiados, caso contrário o método lança um ArgumentOutOfRangeException.
sourceIndex e destinationIndex são baseados em zero.