GroupCollection.CopyTo GroupCollection.CopyTo GroupCollection.CopyTo GroupCollection.CopyTo Method

定义

重载

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

将集合的所有元素复制到给定数组中(从给定索引处开始)。Copies all the elements of the collection to the given array beginning at the given index.

CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32)

从特定的数组索引开始,将组集合的元素复制到 System.Text.RegularExpressions.Group[] 数组中。Copies the elements of the group collection to a System.Text.RegularExpressions.Group[] array, starting at a particular array index.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

将集合的所有元素复制到给定数组中(从给定索引处开始)。Copies all the elements of the collection to the given array beginning at the given index.

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

参数

array
Array Array Array Array

要将集合复制到其中的数组。The array the collection is to be copied into.

arrayIndex
Int32 Int32 Int32 Int32

目标数组中开始复制的位置。The position in the destination array where the copying is to begin.

实现

异常

arrayIndexarray 的界限外。arrayIndex is outside the bounds of array.

-or-

arrayIndex 以及 Countarray 的界限外。arrayIndex plus Count is outside the bounds of array.

示例

下面的示例从句子提取每个单词并在捕获组中捕获它, CopyTo然后使用方法将每个匹配的GroupCollection对象中的元素复制到一个数组,该数组包含来自所有匹配项的捕获组。The following example extracts each word from a sentence and captures it in a capturing group, The CopyTo method is then used to copy the elements in each match's GroupCollection object to an array that contains the capturing groups from all matches. 然后,会向控制台显示单个捕获的字词。The individual captured words are then displayed to the console.

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

正则表达式的定义如下:The regular expression is defined as follows:

模式Pattern 说明Description
\b 与字边界匹配。Match a word boundary.
(\S+?) 匹配一个或多个非空白字符。Match one or more non-white space characters. 将其分配给第一个捕获组。Assign them to the first capturing group.
\b 与字边界匹配。Match a word boundary.

注解

由于整个集合从给定索引处开始复制到数组中,因此目标数组的大小必须至少与集合大。Because the entire collection is copied into the array starting at the given index, the destination array must be at least as large as the collection.

警告

中不存在此成员可移植类库Portable Class LibraryThis member is not present in the 可移植类库Portable Class Library. 如果要开发面向的可移植类库Portable Class Library应用程序,请GroupCollection.ICollection.CopyTo改用方法。If you are developing applications that target the 可移植类库Portable Class Library, use the GroupCollection.ICollection.CopyTo method instead.

CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32) CopyTo(Group[], Int32)

从特定的数组索引开始,将组集合的元素复制到 System.Text.RegularExpressions.Group[] 数组中。Copies the elements of the group collection to a System.Text.RegularExpressions.Group[] array, starting at a particular array index.

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[]

作为组集合中元素的复制目标位置的一维数组。The one-dimensional array that is the destination of the elements copied from the group collection. 该数组的索引必须从零开始。The array must have zero-based indexing.

arrayIndex
Int32 Int32 Int32 Int32

array 中从零开始的索引,从此处开始复制。The zero-based index in array at which copying begins.

异常

arrayIndex 小于零。arrayIndex is less than zero.

- 或 --or-

arrayIndex 大于 array 的长度。arrayIndex is greater than the length of array.

array - arrayIndex 的长度小于组集合计数。The length of array - arrayIndex is less than the group collection count.

适用于