String.CopyTo メソッド

定義

オーバーロード

名前 説明
CopyTo(Span<Char>)

この文字列の内容をコピー先スパンにコピーします。

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

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

CopyTo(Span<Char>)

ソース:
String.cs
ソース:
String.cs
ソース:
String.cs
ソース:
String.cs
ソース:
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)

ソース:
String.cs
ソース:
String.cs
ソース:
String.cs
ソース:
String.cs
ソース:
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です。

sourceIndexdestinationIndex、または count が負の値である

-又は-

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 は 0 から始まります。

こちらもご覧ください

適用対象