String.CopyTo Methode

Definition

Überlädt

CopyTo(Span<Char>)

Kopiert den Inhalt dieser Zeichenfolge in die Zielspanne.

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

Kopiert eine angegebene Anzahl von Zeichen von einer angegebenen Position in dieser Instanz an eine angegebene Position in einem Array von Unicode-Zeichen.

CopyTo(Span<Char>)

Kopiert den Inhalt dieser Zeichenfolge in die Zielspanne.

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

Parameter

destination
Span<Char>

Der Bereich, in den der Inhalt dieser Zeichenfolge kopiert werden soll.

Ausnahmen

Die Zielspanne ist kürzer als die Quellzeichenfolge.

Gilt für:

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

Kopiert eine angegebene Anzahl von Zeichen von einer angegebenen Position in dieser Instanz an eine angegebene Position in einem Array von Unicode-Zeichen.

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)

Parameter

sourceIndex
Int32

Der Index des ersten Zeichens in dieser Instanz, das kopiert werden soll.

destination
Char[]

Ein Array von Unicode-Zeichen, in das Zeichen in dieser Instanz kopiert werden.

destinationIndex
Int32

Der Index in destination, bei dem der Kopiervorgang beginnt.

count
Int32

Die Anzahl der Zeichen in dieser Instanz, die nach destination kopiert werden sollen.

Ausnahmen

destination ist null.

sourceIndex, destinationIndex oder count ist ein negativer Wert.

- oder -

sourceIndex identifiziert keine Position in der aktuellen Instanz.

- oder -

destinationIndex identifiziert keinen gültigen Index im destination-Array.

- oder -

count ist größer als die Länge der Teilzeichenfolge von sourceIndex bis zum Ende dieser Instanz

- oder -

count ist größer als die Länge des Teilarrays von destinationIndex bis zum Ende des destination-Arrays.

Beispiele

Das folgende Beispiel veranschaulicht die CopyTo Methode.

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

Hinweise

Diese Methode kopiert count Zeichen von der sourceIndex Position dieser Instanz an die destinationIndex Position des destination Zeichenarrays. Diese Methode ändert die Größe des destination Zeichenarrays nicht. Sie muss über eine ausreichende Anzahl von Elementen verfügen, um die kopierten Zeichen aufzunehmen, oder die -Methode löst eine aus ArgumentOutOfRangeException.

sourceIndex und destinationIndex sind nullbasiert.

Weitere Informationen

Gilt für: