StringBuilder.CopyTo メソッド

定義

オーバーロード

CopyTo(Int32, Span<Char>, Int32)

文字をこのインスタンスの指定したセグメントから目的の Char のスパンにコピーします。

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

このインスタンスの指定したセグメントにある文字を、目的の Char 配列の指定したセグメントにコピーします。

CopyTo(Int32, Span<Char>, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

文字をこのインスタンスの指定したセグメントから目的の Char のスパンにコピーします。

public:
 void CopyTo(int sourceIndex, Span<char> destination, int count);
public void CopyTo (int sourceIndex, Span<char> destination, int count);
member this.CopyTo : int * Span<char> * int -> unit
Public Sub CopyTo (sourceIndex As Integer, destination As Span(Of Char), count As Integer)

パラメーター

sourceIndex
Int32

このインスタンスにおける文字のコピーの開始位置。 インデックスの値は、0 から始まります。

destination
Span<Char>

文字のコピー先となる書き込み可能なスパン。

count
Int32

コピーする文字数。

注釈

メソッドは CopyTo 、オブジェクトの連続するセクションをスパンに効率的にコピーする必要があるまれな StringBuilder 状況で使用することを目的としています。

たとえば、コードでオブジェクトに多数の文字を設定 StringBuilder した後、 メソッドを CopyTo 使用して、小さな連続するオブジェクトを、その部分 StringBuilder が処理されるスパンにコピーできます。 オブジェクト内 StringBuilder のすべてのデータが処理されると、オブジェクトの StringBuilder サイズが 0 に設定され、サイクルが繰り返されます。

適用対象

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

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

このインスタンスの指定したセグメントにある文字を、目的の Char 配列の指定したセグメントにコピーします。

public:
 void CopyTo(int sourceIndex, cli::array <char> ^ destination, int destinationIndex, int count);
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
[System.Runtime.InteropServices.ComVisible(false)]
public void CopyTo (int sourceIndex, char[] destination, int destinationIndex, int count);
member this.CopyTo : int * char[] * int * int -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
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

このインスタンスにおける文字のコピーの開始位置。 インデックスの値は、0 から始まります。

destination
Char[]

文字のコピー先となる配列。

destinationIndex
Int32

destination における文字のコピーの開始位置。 インデックスの値は、0 から始まります。

count
Int32

コピーする文字数。

属性

例外

destinationnullです。

sourceIndexdestinationIndex または count が 0 より小さい値です。

- または -

sourceIndex はこのインスタンスの長さを超えています。

sourceIndex + count がこのインスタンスの長さを超えています。

- または -

destinationIndex + countdestination の長さを超えています。

CopyToメソッドの例を次に示します。

// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.
// Typically the destination array is small, preallocated, and global while 
// the StringBuilder is large with programmatically defined data. 
// However, for this example both the array and StringBuilder are small 
// and the StringBuilder has predefined data.

using namespace System;
using namespace System::Text;

int main()
{
   array<Char>^dest = gcnew array<Char>(6);
   StringBuilder^ src = gcnew StringBuilder( "abcdefghijklmnopqrstuvwxyz!" );
   dest[ 1 ] = ')';
   dest[ 2 ] = ' ';

   // Copy the source to the destination in 9 pieces, 3 characters per piece.
   Console::WriteLine( "\nPiece) Data:" );
   for ( int ix = 0; ix < 9; ix++ )
   {
      dest[ 0 ] = ix.ToString()[ 0 ];
      src->CopyTo( ix * 3, dest, 3, 3 );
      Console::Write( "    " );
      Console::WriteLine( dest );
   }
}

/*
This example produces the following results:

Piece) Data:
    0) abc
    1) def
    2) ghi
    3) jkl
    4) mno
    5) pqr
    6) stu
    7) vwx
    8) yz!
*/
// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.

// Typically the destination array is small, preallocated, and global while
// the StringBuilder is large with programmatically defined data.
// However, for this example both the array and StringBuilder are small
// and the StringBuilder has predefined data.

using System;
using System.Text;

class Sample
{
    protected static char[] dest = new char[6];
    public static void Main()
    {
    StringBuilder src = new StringBuilder("abcdefghijklmnopqrstuvwxyz!");
    dest[1] = ')';
    dest[2] = ' ';

// Copy the source to the destination in 9 pieces, 3 characters per piece.

    Console.WriteLine("\nPiece) Data:");
    for(int ix = 0; ix < 9; ix++)
        {
        dest[0] = ix.ToString()[0];
        src.CopyTo(ix * 3, dest, 3, 3);
        Console.Write("    ");
        Console.WriteLine(dest);
        }
    }
}
/*
This example produces the following results:

Piece) Data:
    0) abc
    1) def
    2) ghi
    3) jkl
    4) mno
    5) pqr
    6) stu
    7) vwx
    8) yz!
*/
// This example demonstrates the CopyTo(Int32, Char[], Int32, Int32) method.

// Typically the destination array is small, preallocated, and global while
// the StringBuilder is large with programmatically defined data.
// However, for this example both the array and StringBuilder are small
// and the StringBuilder has predefined data.

open System.Text

let dest = Array.zeroCreate 6

let src = StringBuilder "abcdefghijklmnopqrstuvwxyz!"
dest[1] <- ')'
dest[2] <- ' '

// Copy the source to the destination in 9 pieces, 3 characters per piece.

printfn "\Piece) Data:"
for i = 0 to 8 do
    dest[0] <- (string i)[0]
    src.CopyTo(i * 3, dest, 3, 3)
    printfn $"    {dest}"

// This example produces the following results:
//       Piece) Data:
//           0) abc
//           1) def
//           2) ghi
//           3) jkl
//           4) mno
//           5) pqr
//           6) stu
//           7) vwx
//           8) yz!
' Typically the destination array is small, preallocated, and global while 
' the StringBuilder is large with programmatically defined data. 
' However, for this example both the array and StringBuilder are small 
' and the StringBuilder has predefined data.

Imports System.Text

Class Sample
   Protected Shared dest(5) As Char
   
   Public Shared Sub Main()
      Dim src As New StringBuilder("abcdefghijklmnopqrstuvwxyz!")
      dest(1) = ")"c
      dest(2) = " "c
      
      ' Copy the source to the destination in 9 pieces, 3 characters per piece.
      Console.WriteLine(vbCrLf & "Piece) Data:")
      Dim ix As Integer
      For ix = 0 To 8
         dest(0) = ix.ToString()(0)
         src.CopyTo(ix * 3, dest, 3, 3)
         Console.Write("    ")
         Console.WriteLine(dest)
      Next ix
   End Sub
End Class
'
' This example produces the following results:
'
' Piece) Data:
'     0) abc
'     1) def
'     2) ghi
'     3) jkl
'     4) mno
'     5) pqr
'     6) stu
'     7) vwx
'     8) yz!

注釈

メソッドは CopyTo 、オブジェクトの連続するセクション StringBuilder を配列に効率的にコピーする必要があるまれな状況で使用することを目的としています。 配列は、固定サイズ、事前割り当て済み、再利用可能、および場合によってはグローバルにアクセスできる必要があります。

たとえば、コードでは、オブジェクトに StringBuilder 多数の文字を設定してから、 メソッドを CopyTo 使用して、小さな連続するオブジェクトを、その部分 StringBuilder が処理される配列にコピーできます。 オブジェクト内 StringBuilder のすべてのデータが処理されると、オブジェクトの StringBuilder サイズが 0 に設定され、サイクルが繰り返されます。

適用対象