String.CopyTo Metoda

Definice

Přetížení

CopyTo(Span<Char>)

Zkopíruje obsah tohoto řetězce do cílového rozsahu.

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

Zkopíruje zadaný počet znaků ze zadané pozice v této instanci na zadanou pozici v poli znaků Unicode.

CopyTo(Span<Char>)

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

Zkopíruje obsah tohoto řetězce do cílového rozsahu.

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

Parametry

destination
Span<Char>

Rozsah, do kterého se má kopírovat obsah tohoto řetězce.

Výjimky

Cílový rozsah je kratší než zdrojový řetězec.

Platí pro

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

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

Zkopíruje zadaný počet znaků ze zadané pozice v této instanci na zadanou pozici v poli znaků 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)

Parametry

sourceIndex
Int32

Index prvního znaku v této instanci, který se má zkopírovat.

destination
Char[]

Pole znaků Unicode, do kterého jsou znaky v této instanci zkopírovány.

destinationIndex
Int32

Index, ve destination kterém začíná operace kopírování.

count
Int32

Počet znaků v této instanci, které se mají zkopírovat do destination.

Výjimky

destination je null.

sourceIndex, destinationIndexnebo count je negativní

-nebo-

sourceIndex neidentifikuje pozici v aktuální instanci.

-nebo-

destinationIndex neidentifikuje platný index v destination poli.

-nebo-

count je větší než délka podřetěžce od sourceIndex konce této instance.

-nebo-

count je větší než délka podadresy od destinationIndex konce destination pole.

Příklady

Následující příklad ukazuje metodu CopyTo .

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

Poznámky

Tato metoda kopíruje count znaky z sourceIndex pozice této instance do destinationIndex pozice destination pole znaků. Tato metoda nemění velikost destination pole znaků; musí mít dostatečný počet prvků pro umístění zkopírovaných znaků nebo metoda vyvolá ArgumentOutOfRangeException.

sourceIndex a destinationIndex jsou založené na nule.

Viz také

Platí pro