开发者社区 问答 正文

将MatchCollection转换为字符串数组

有比这更好的方法来将MatchCollection转换为字符串数组吗?

MatchCollection mc = Regex.Matches(strText, @"\b[A-Za-z-']+\b"); string[] strArray = new string[mc.Count]; for (int i = 0; i < mc.Count;i++ ) { strArray[i] = mc[i].Groups[0].Value; } PS:mc.CopyTo(strArray,0)引发异常:

无法将源数组中的至少一个元素转换为目标数组类型。 问题来源于stack overflow

展开
收起
保持可爱mmm 2020-02-08 21:41:50 678 分享 版权
1 条回答
写回答
取消 提交回答
  • 尝试:

    var arr = Regex.Matches(strText, @"\b[A-Za-z-']+\b") .Cast () .Select(m => m.Value) .ToArray();

    2020-02-08 21:41:59
    赞同 展开评论
问答地址: