开发者社区> 问答> 正文

将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 630 0
1 条回答
写回答
取消 提交回答
  • 尝试:

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

    2020-02-08 21:41:59
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载