String.CopyTo 方法

定義

多載

名稱 Description
CopyTo(Span<Char>)

將此字串的內容複製到目的區間。

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

在此實例中,將指定數量的字元從指定位置複製到一個 Unicode 字元陣列中的指定位置。

CopyTo(Span<Char>)

來源:
String.cs
來源:
String.cs
來源:
String.cs
來源:
String.cs
來源:
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)

來源:
String.cs
來源:
String.cs
來源:
String.cs
來源:
String.cs
來源:
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

sourceIndexdestinationIndex, 或 count 為負

-或-

sourceIndex 不識別當前實例中的位置。

-或-

destinationIndex 無法在陣列中識別有效的索引 destination

-或-

count 大於從 sourceIndex 該實例到末端的子字串長度

-或-

count 大於子陣列從 destinationIndex 到陣 destination 列末端的長度。

範例

以下範例示範此 CopyTo 方法。

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 皆為零基。

另請參閱

適用於