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

복사될 문자 수입니다.

특성

예외

destination이(가) null인 경우

sourceIndex, destinationIndex 또는 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으로 설정되고 주기가 반복됩니다.

적용 대상