String.CopyTo Metode

Definisi

Overload

CopyTo(Span<Char>)

Menyalin isi string ini ke dalam rentang tujuan.

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

Menyalin sejumlah karakter tertentu dari posisi tertentu dalam instans ini ke posisi tertentu dalam array karakter Unicode.

CopyTo(Span<Char>)

Sumber:
String.cs
Sumber:
String.cs
Sumber:
String.cs

Menyalin isi string ini ke dalam rentang tujuan.

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>

Rentang untuk menyalin konten string ini.

Pengecualian

Rentang tujuan lebih pendek dari string sumber.

Berlaku untuk

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

Sumber:
String.cs
Sumber:
String.cs
Sumber:
String.cs

Menyalin sejumlah karakter tertentu dari posisi tertentu dalam instans ini ke posisi tertentu dalam array karakter 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)

Parameter

sourceIndex
Int32

Indeks karakter pertama dalam instans ini untuk disalin.

destination
Char[]

Array karakter Unicode tempat karakter dalam instans ini disalin.

destinationIndex
Int32

Indeks tempat destination operasi penyalinan dimulai.

count
Int32

Jumlah karakter dalam instans ini untuk disalin ke destination.

Pengecualian

destinationadalah null.

sourceIndex, , destinationIndexatau count negatif

-atau-

sourceIndex tidak mengidentifikasi posisi dalam instans saat ini.

-atau-

destinationIndex tidak mengidentifikasi indeks yang valid dalam destination array.

-atau-

count lebih besar dari panjang substring dari sourceIndex hingga akhir instans ini

-atau-

count lebih besar dari panjang subarray dari destinationIndex ke akhir destination array.

Contoh

Contoh berikut menunjukkan CopyTo metode .

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

Keterangan

Metode ini menyalin count karakter dari sourceIndex posisi instans ini ke destinationIndex posisi destination array karakter. Metode ini tidak mengubah ukuran destination array karakter; harus memiliki jumlah elemen yang memadai untuk mengakomodasi karakter yang disalin atau metode melempar ArgumentOutOfRangeException.

sourceIndex dan destinationIndex berbasis nol.

Lihat juga

Berlaku untuk