String.CopyTo Metoda

Definicja

Przeciążenia

CopyTo(Span<Char>)

Kopiuje zawartość tego ciągu do zakresu docelowego.

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

Kopiuje określoną liczbę znaków z określonej pozycji w tym wystąpieniu do określonej pozycji w tablicy znaków Unicode.

CopyTo(Span<Char>)

Źródło:
String.cs
Źródło:
String.cs
Źródło:
String.cs

Kopiuje zawartość tego ciągu do zakresu docelowego.

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>

Zakres, w którym chcesz skopiować zawartość tego ciągu.

Wyjątki

Zakres docelowy jest krótszy niż ciąg źródłowy.

Dotyczy

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

Źródło:
String.cs
Źródło:
String.cs
Źródło:
String.cs

Kopiuje określoną liczbę znaków z określonej pozycji w tym wystąpieniu do określonej pozycji w tablicy znaków 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

Indeks pierwszego znaku w tym wystąpieniu do skopiowania.

destination
Char[]

Tablica znaków Unicode, do której są kopiowane znaki w tym wystąpieniu.

destinationIndex
Int32

Indeks, w destination którym rozpoczyna się operacja kopiowania.

count
Int32

Liczba znaków w tym wystąpieniu do skopiowania do destinationelementu .

Wyjątki

destination to null.

sourceIndex, destinationIndexlub count jest ujemny

-lub-

sourceIndex nie identyfikuje pozycji w bieżącym wystąpieniu.

-lub-

destinationIndex nie identyfikuje prawidłowego indeksu w tablicy destination .

-lub-

count jest większa niż długość podciągów od sourceIndex do końca tego wystąpienia

-lub-

count jest większa niż długość podarray od destinationIndex do końca tablicy destination .

Przykłady

W poniższym przykładzie pokazano metodę 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

Uwagi

Ta metoda kopiuje count znaki z sourceIndex położenia tego wystąpienia do destinationIndex pozycji tablicy destination znaków. Ta metoda nie zmienia rozmiaru destination tablicy znaków; musi mieć wystarczającą liczbę elementów, aby pomieścić skopiowane znaki lub metoda zgłasza wartość ArgumentOutOfRangeException.

sourceIndex i destinationIndex są oparte na zerach.

Zobacz też

Dotyczy