matchCollection遍历的疑惑!
可是呢,用for循环的话
for (int i = 0; i < matches.Count; i++)
{
arr[0, i] = m.Index;
arr[1, i] = m.Index + m.Length;
}
foreach (Match m in Matches)
{
arr[0, i] = m.Index;
arr[1, i] = m.Index + m.Length;
}
string s = "你好,请问这个要怎么做呢?谢谢了,非常感谢!";
MatchCollection matches = Regex.Matches(s, @"谢");
int[,] arr = new int[2, matches.Count];
for (int i = 0; i < matches.Count; i++)
{
arr[0, i] = matches[i].Index;
arr[1, i] = matches[i].Index + matches[i].Length;
}
//以下是输出:
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
Console.Write(arr[i, j] + " ");
Console.WriteLine();
}