String.CopyTo 方法

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

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Sub CopyTo ( _
    sourceIndex As Integer, _
    destination As Char(), _
    destinationIndex As Integer, _
    count As Integer _
)
用法
Dim instance As String
Dim sourceIndex As Integer
Dim destination As Char()
Dim destinationIndex As Integer
Dim count As Integer

instance.CopyTo(sourceIndex, destination, destinationIndex, count)
public void CopyTo (
    int sourceIndex,
    char[] destination,
    int destinationIndex,
    int count
)
public:
void CopyTo (
    int sourceIndex, 
    array<wchar_t>^ destination, 
    int destinationIndex, 
    int count
)
public void CopyTo (
    int sourceIndex, 
    char[] destination, 
    int destinationIndex, 
    int count
)
public function CopyTo (
    sourceIndex : int, 
    destination : char[], 
    destinationIndex : int, 
    count : int
)

参数

  • sourceIndex
    此实例中的字符位置。
  • destination
    Unicode 字符的数组。
  • destinationIndex
    destination 中的数组元素。
  • count
    此实例中要复制到 destination 的字符数。

异常

异常类型 条件

ArgumentNullException

destination 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

sourceIndex、destinationIndex 或 count 为负

- 或 -

count 大于从 startIndex 到此实例末尾的子字符串的长度

- 或 -

count 大于从 destinationIndex 到 destination 末尾的子数组的长度

备注

count 字符从此实例的 sourceIndex 位置复制到 destination 的 destinationIndex 位置。

sourceIndex 和 destinationIndex 是从零开始的。

示例

下面的代码示例说明了 CopyTo 方法。

Imports System

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 'Main
End Class 'CopyToTest
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 );
    }
}
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 );
}
import System.*;

public class CopyToTest
{
    public static void main(String[] args)
    {
        // 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.get_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);
    } //main
} //CopyToTest
import System;

public class CopyToTest {
    public static function Main() : void {

        // Embed an array of characters in a string
        var strSource : String = "changed";
        var destination : char[] = "The initial array".ToCharArray();

        // 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 );
    }
}
CopyToTest.Main();

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

String 类
String 成员
System 命名空间
Char 结构
Int32 结构
Insert
Substring