String.CopyTo 方法

定义

重载

CopyTo(Span<Char>)

将此字符串的内容复制到目标范围。

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

将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。

CopyTo(Span<Char>)

将此字符串的内容复制到目标范围。

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)

将指定数目的字符从此实例中的指定位置复制到 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 从零开始。

另请参阅

适用于