String.CopyTo Méthode

Définition

Surcharges

Nom Description
CopyTo(Span<Char>)

Copie le contenu de cette chaîne dans l’étendue de destination.

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

Copie un nombre spécifié de caractères d’une position spécifiée dans cette instance vers une position spécifiée dans un tableau de caractères Unicode.

CopyTo(Span<Char>)

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

Copie le contenu de cette chaîne dans l’étendue de destination.

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

Paramètres

destination
Span<Char>

Étendue dans laquelle copier le contenu de cette chaîne.

Exceptions

L’étendue de destination est plus courte que la chaîne source.

S’applique à

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

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

Copie un nombre spécifié de caractères d’une position spécifiée dans cette instance vers une position spécifiée dans un tableau de caractères 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)

Paramètres

sourceIndex
Int32

Index du premier caractère de cette instance à copier.

destination
Char[]

Tableau de caractères Unicode dans lequel les caractères de cette instance sont copiés.

destinationIndex
Int32

Index dans destination lequel l’opération de copie commence.

count
Int32

Nombre de caractères dans cette instance à copier vers destination.

Exceptions

destination a la valeur null.

sourceIndex, destinationIndexou count est négatif

- ou -

sourceIndex n’identifie pas une position dans l’instance actuelle.

- ou -

destinationIndex n’identifie pas d’index valide dans le destination tableau.

- ou -

count est supérieur à la longueur de la sous-chaîne de sourceIndex la fin de cette instance

- ou -

count est supérieur à la longueur du sous-tableau de destinationIndex la fin du destination tableau.

Exemples

L’exemple suivant illustre la CopyTo méthode.

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

Remarques

Cette méthode copie les count caractères de la sourceIndex position de cette instance à la destinationIndex position du tableau de destination caractères. Cette méthode ne redimensionne pas le destination tableau de caractères ; elle doit avoir un nombre suffisant d’éléments pour prendre en charge les caractères copiés ou la méthode lève un ArgumentOutOfRangeException.

sourceIndex et destinationIndex sont de base zéro.

Voir aussi

S’applique à