String.CopyTo 方法

定义

重载

名称 说明
CopyTo(Span<Char>)

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

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

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

CopyTo(Span<Char>)

Source:
String.cs
Source:
String.cs
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
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 大于从 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 从零开始。

另请参阅

适用于