String.CopyTo Metodo

Definizione

Overload

CopyTo(Span<Char>)

Copia il contenuto di questa stringa nell'intervallo di destinazione.

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

Copia un numero definito di caratteri da una posizione specificata in questa istanza in una posizione specificata in una matrice di caratteri Unicode.

CopyTo(Span<Char>)

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

Copia il contenuto di questa stringa nell'intervallo di destinazione.

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))

Parametri

destination
Span<Char>

Intervallo in cui copiare il contenuto di questa stringa.

Eccezioni

L'intervallo di destinazione è più breve della stringa di origine.

Si applica a

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

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

Copia un numero definito di caratteri da una posizione specificata in questa istanza in una posizione specificata in una matrice di caratteri 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)

Parametri

sourceIndex
Int32

Indice del primo carattere nell'istanza da copiare.

destination
Char[]

Matrice di caratteri Unicode in cui vengono copiati i caratteri di questa istanza.

destinationIndex
Int32

Indice nel parametro destination in corrispondenza del quale inizia l'operazione di copia.

count
Int32

Numero di caratteri dell'istanza da copiare in destination.

Eccezioni

destination è null.

sourceIndex, destinationIndex o count è negativo.

-oppure-

sourceIndex non identifica una posizione nell'istanza corrente.

-oppure-

destinationIndex non identifica un indice valido nella matrice destination.

-oppure-

count è maggiore della lunghezza della sottostringa compresa fra l'indice specificato nel parametro sourceIndex e la fine dell'istanza.

-oppure-

count è maggiore della lunghezza della sottomatrice compresa fra il valore del parametro destinationIndex e la fine della matrice destination.

Esempio

Nell'esempio seguente viene illustrato il CopyTo metodo.

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

Commenti

Questo metodo copia count i caratteri dalla sourceIndex posizione di questa istanza alla destinationIndex posizione della matrice di destination caratteri. Questo metodo non ridimensiona la destination matrice di caratteri. Deve avere un numero sufficiente di elementi per contenere i caratteri copiati o il metodo genera un ArgumentOutOfRangeExceptionoggetto .

sourceIndex e destinationIndex sono basati su zero.

Vedi anche

Si applica a