GroupCollection.CopyTo 메서드

정의

오버로드

CopyTo(Array, Int32)

지정된 인덱스에서 시작하여 지정된 배열에 컬렉션의 요소를 모두 복사합니다.

CopyTo(Group[], Int32)

특정 배열 인덱스부터 시작하여 그룹 컬렉션의 요소를 Group 배열에 복사합니다.

CopyTo(Array, Int32)

지정된 인덱스에서 시작하여 지정된 배열에 컬렉션의 요소를 모두 복사합니다.

public:
 virtual void CopyTo(Array ^ array, int arrayIndex);
public void CopyTo (Array array, int arrayIndex);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (array As Array, arrayIndex As Integer)

매개 변수

array
Array

컬렉션을 복사할 대상 배열입니다.

arrayIndex
Int32

대상 배열에서 복사를 시작할 위치입니다.

구현

예외

array이(가) null인 경우

arrayIndexarray의 범위 밖인 경우

또는

arrayIndexCount를 더한 값이 array의 범위 밖인 경우

예제

다음은 문장에서 각 단어를 추출하여 캡처링 그룹에 캡처하는 예제입니다. 그런 다음 이 CopyTo 메서드는 각 일치 항목의 개체에 있는 요소를 모든 일치 항목의 GroupCollection 캡처링 그룹이 포함된 배열에 복사하는 데 사용됩니다. 그런 다음 캡처된 개별 단어가 콘솔에 표시됩니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\S+?)\b";
      string input = "This sentence is rather short but pointless.";

      MatchCollection matches = Regex.Matches(input, pattern);
      object[] words = new object[matches.Count * 2];
      
      int index = 0;
      foreach (Match match in matches)
      {
         match.Groups.CopyTo(words, index);
         index += 2;
      }
      
      // Display captured groups.
      for (int ctr = 1; ctr <= words.GetUpperBound(0); ctr += 2)
         Console.WriteLine(words[ctr]);
   }
}
// The example displays the following output:
//       This
//       sentence
//       is
//       rather
//       short
//       but
//       pointless
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\S+?)\b"
      Dim input As String = "This sentence is rather short but pointless."

      Dim matches As MatchCollection = Regex.Matches(input, pattern)
      Dim words(matches.Count * 2 - 1) As Object
      
      Dim index As Integer = 0
      For Each match As Match In matches
         match.Groups.CopyTo(words, index)
         index += 2
      Next
      ' Display captured groups.
      For ctr As Integer = 1 To words.GetUpperBound(0) Step 2
         Console.WriteLine(words(ctr))
      Next
   End Sub
End Module
' The example displays the following output:
'       This
'       sentence
'       is
'       rather
'       short
'       but
'       pointless

정규식은 다음과 같이 정의됩니다.

무늬 설명
\b 단어 경계를 찾습니다.
(\S+?) 공백이 아닌 문자를 하나 이상 찾습니다. 첫 번째 캡처링 그룹에 할당합니다.
\b 단어 경계를 찾습니다.

설명

전체 컬렉션이 지정된 인덱스에서 시작하는 배열에 복사되므로 대상 배열은 컬렉션만큼 커야 합니다.

경고

이 멤버는 이식 가능한 클래스 라이브러리에 없습니다. 이식 가능한 클래스 라이브러리를 대상으로 하는 애플리케이션을 개발하는 경우 대신 메서드를 GroupCollection.ICollection.CopyTo 사용합니다.

적용 대상

CopyTo(Group[], Int32)

특정 배열 인덱스부터 시작하여 그룹 컬렉션의 요소를 Group 배열에 복사합니다.

public:
 virtual void CopyTo(cli::array <System::Text::RegularExpressions::Group ^> ^ array, int arrayIndex);
public void CopyTo (System.Text.RegularExpressions.Group[] array, int arrayIndex);
abstract member CopyTo : System.Text.RegularExpressions.Group[] * int -> unit
override this.CopyTo : System.Text.RegularExpressions.Group[] * int -> unit
Public Sub CopyTo (array As Group(), arrayIndex As Integer)

매개 변수

array
Group[]

그룹 컬렉션에서 복사한 요소의 대상인 1차원 배열입니다. 배열에는 0부터 시작하는 인덱스가 있어야 합니다.

arrayIndex
Int32

array에서 복사가 시작되는 0부터 시작하는 인덱스입니다.

구현

예외

array가 null입니다.

arrayIndex가 0보다 작은 경우

또는

arrayIndexarray의 길이보다 큽니다.

array - arrayIndex의 길이가 그룹 컬렉션 수보다 작습니다.

적용 대상