GroupCollection.CopyTo メソッド

定義

オーバーロード

名前 説明
CopyTo(Array, Int32)

コレクションのすべての要素を、指定したインデックスから始まる指定した配列にコピーします。

CopyTo(Group[], Int32)

グループ コレクションの要素を、特定の配列インデックスから始まる Group 配列にコピーします。

CopyTo(Array, Int32)

ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs

コレクションのすべての要素を、指定したインデックスから始まる指定した配列にコピーします。

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

コピーを開始するコピー先配列内の位置。

実装

例外

arraynullです。

arrayIndexarrayの範囲外です。 または、 arrayIndex プラス Countarrayの範囲外です。

次の例では、文から各単語を抽出し、キャプチャ グループにキャプチャします。 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

正規表現は次のように定義されます。

Pattern Description
\b ワード境界に一致させる。
(\S+?) 1 つ以上の空白以外の文字と一致します。 それらを最初のキャプチャ グループに割り当てます。
\b ワード境界に一致させる。

注釈

コレクション全体が指定されたインデックスから始まる配列にコピーされるため、コピー先の配列は少なくともコレクションと同じ大きさにする必要があります。

Warning

このメンバーはポータブル クラス ライブラリには存在しません。 ポータブル クラス ライブラリを対象とするアプリケーションを開発する場合は、代わりに GroupCollection.ICollection.CopyTo メソッドを使用します。

適用対象

CopyTo(Group[], Int32)

ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs
ソース:
GroupCollection.cs

グループ コレクションの要素を、特定の配列インデックスから始まる 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 から始まるインデックス。

実装

例外

arraynullです。

arrayIndex が 0 未満です。 または、 arrayIndexarrayの長さを超えています。

array - arrayIndexの長さは、グループ コレクションの数より小さいです。

適用対象