String.CopyTo 方法

定義

多載

CopyTo(Span<Char>)

將這個字串的內容複寫到目的地範圍。

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

將字元的指定數目從這個執行個體的指定位置,複製到 Unicode 字元陣列的指定位置。

CopyTo(Span<Char>)

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

將這個字串的內容複寫到目的地範圍。

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

參數

destination
Span<Char>

要複製此字串內容的範圍。

例外狀況

目的地範圍比來源字串短。

適用於

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

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

將字元的指定數目從這個執行個體的指定位置,複製到 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)

參數

sourceIndex
Int32

這個要複製的執行個體中第一個字元的索引。

destination
Char[]

複製這個執行個體之字元的目標 Unicode 字元陣列。

destinationIndex
Int32

destination 中開始複製作業的索引。

count
Int32

這個執行個體中要複製到 destination 的字元數。

例外狀況

destinationnull

sourceIndexdestinationIndexcount 為負。

-或-

sourceIndex 未識別目前執行個體中的位置。

-或-

destinationIndex 未識別 destination 陣列中的有效索引。

-或-

count 大於從 sourceIndex 到這個執行個體結尾的子字串長度。

-或-

count 大於從 destinationIndexdestination 陣列結尾的子陣列長度。

範例

下列範例示範 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

備註

這個方法會將 count 字元從 sourceIndex 這個實例的位置複製到 destinationIndex 字元陣列的位置 destination 。 這個方法不會調整字元陣列的大小 destination ;它必須有足夠的元素數目,才能容納複製的字元,或方法會 ArgumentOutOfRangeException 擲回 。

sourceIndexdestinationIndex 是以零起始。

另請參閱

適用於