String.CopyTo メソッドとは? わかりやすく解説

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

destinationnull 参照 (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
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();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からString.CopyTo メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からString.CopyTo メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からString.CopyTo メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

String.CopyTo メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.CopyTo メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS