String.CopyTo 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
CopyTo(Span<Char>) |
将此字符串的内容复制到目标范围。 |
CopyTo(Int32, Char[], Int32, Int32) |
将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。 |
CopyTo(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
的字符数。
例外
destination
为 null
。
sourceIndex
、destinationIndex
或 count
为负
或
sourceIndex
不标识当前实例中的位置。
或
destinationIndex
不标识 destination
数组中的有效索引。
或
count
大于从 sourceIndex
到此实例末尾的子字符串的长度
或
count
大于从 destinationIndex
到 destination
数组末尾的子数组的长度。
示例
下面的示例演示 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引发 。
sourceIndex
和 destinationIndex
从零开始。