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

在目的陣列中要開始複製的位置。

實作

例外狀況

arraynull

arrayIndex 不在 array 範圍內。

-或-

arrayIndex 加上 Count 落在 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[]

從群組集合複製元素的目的地一維陣列。 陣列必須有以零為起始的索引。

arrayIndex
Int32

array 中以零起始的索引,即開始複製的位置。

實作

例外狀況

array 為 null。

arrayIndex 小於零。

-或-

arrayIndex 大於 array 的長度。

array - arrayIndex 的長度小於群組集合計數。

適用於