如何MatchCollection转换为字符串数组 [英] How to convert MatchCollection to string array

查看:484
本文介绍了如何MatchCollection转换为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方式来MatchCollection转换为字符串数组比:

Is there a better way to convert MatchCollection to string array than that:

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;
  }

P.S:mc.CopyTo(strArray,0)施放一个例外:源数组中至少有一个元素不能被抛弃到目标数组类型

P.S.: mc.CopyTo(strArray,0) casts an exception: "At least one element in the source array could not be cast down to the destination array type."

推荐答案

尝试:

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

这篇关于如何MatchCollection转换为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆