Share via


String.CopyTo メソッド

このインスタンスの指定位置から指定した数の文字を、Unicode 文字の配列内の指定位置へコピーします。

Public Sub CopyTo( _
   ByVal sourceIndex As Integer, _   ByVal destination() As Char, _   ByVal destinationIndex As Integer, _   ByVal count As Integer _)
[C#]
public void CopyTo(intsourceIndex,char[] destination,intdestinationIndex,intcount);
[C++]
public: void CopyTo(intsourceIndex,__wchar_tdestination __gc[],intdestinationIndex,intcount);
[JScript]
public function CopyTo(
   sourceIndex : int,destination : Char[],destinationIndex : int,count : int);

パラメータ

  • sourceIndex
    このインスタンス内の文字位置。
  • destination
    Unicode 文字の配列。
  • destinationIndex
    destination の配列要素。
  • count
    destination へコピーされるこのインスタンスの文字の数。

例外

例外の種類 条件
ArgumentNullException destination が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException sourceIndexdestinationIndex 、または count が負の値です。

または

count が、 startIndex からこのインスタンスの末尾までの部分文字の長さより大きい値です。

または

count が、 destinationIndex から destination の末尾までの部分配列の長さより大きい値です。

解説

count に指定されている数の文字が、このインスタンスの sourceIndex の位置から destinationdestinationIndex の位置へコピーされました。

sourceIndexdestinationIndex が 0 から始まっています。

使用例

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

[C#] 
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 );
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;

int main()
{
   // Embed an array of characters in a string
   String* strSource = S"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 = S"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);
}

[JScript] 
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 NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | Char | Int32 | Insert | Substring