StringBuilder.CopyTo メソッド

定義

オーバーロード

CopyTo(Int32, Span<Char>, Int32)
CopyTo(Int32, Char[], Int32, Int32)

このインスタンスの指定したセグメントにある文字を、目的の Char 配列の指定したセグメントにコピーします。Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

CopyTo(Int32, Span<Char>, Int32)

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
destination
Span<Char>
count
Int32

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

このインスタンスの指定したセグメントにある文字を、目的の Char 配列の指定したセグメントにコピーします。Copies the characters from a specified segment of this instance to a specified segment of a destination Char array.

public:
 void CopyTo(int sourceIndex, cli::array <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
Public Sub CopyTo (sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer)

パラメーター

sourceIndex
Int32

このインスタンスにおける文字のコピーの開始位置。The starting position in this instance where characters will be copied from. インデックスの値は、0 から始まります。The index is zero-based.

destination
Char[]

文字のコピー先となる配列。The array where characters will be copied.

destinationIndex
Int32

destination における文字のコピーの開始位置。The starting position in destination where characters will be copied. インデックスの値は、0 から始まります。The index is zero-based.

count
Int32

コピーする文字数。The number of characters to be copied.

属性

例外

destinationnullです。destination is null.

sourceIndexdestinationIndex または count が 0 より小さい値です。sourceIndex, destinationIndex, or count, is less than zero.

- または --or- sourceIndex はこのインスタンスの長さを超えています。sourceIndex is greater than the length of this instance.

sourceIndex + count がこのインスタンスの長さを超えています。sourceIndex + count is greater than the length of this instance.

- または --or- destinationIndex + countdestination の長さを超えています。destinationIndex + count is greater than the length of destination.

メソッドのCopyTo例を次に示します。The following example demonstrates the CopyTo method.

// 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!
*/
' 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オブジェクトの連続するセクションを配列に効率的にコピーする必要がある場合に、まれな状況で使用することを目的としています。The CopyTo method is intended to be used in the rare situation when you need to efficiently copy successive sections of a StringBuilder object to an array. 配列は固定サイズ、事前に割り当てられ、再利用可能で、場合によってはグローバルにアクセス可能である必要があります。The array should be a fixed size, preallocated, reusable, and possibly globally accessible.

たとえば、アプリケーションで多数の文字をStringBuilder含むオブジェクトを設定した後、 CopyToメソッドを使用して、 StringBuilderオブジェクトの小さな部分を、その部分が処理される配列にコピーできます。For example, your application could populate a StringBuilder object with a large number of characters then use the CopyTo method to copy small, successive pieces of the StringBuilder object to an array where the pieces are processed. StringBuilderオブジェクト内のすべてのデータが処理されると、 StringBuilderオブジェクトのサイズが0に設定され、サイクルが繰り返されます。When all the data in the StringBuilder object is processed, the size of the StringBuilder object is set to zero and the cycle is repeated.

適用対象