【问题标题】:linq SelectMany and Regex.Split clrlinq SelectMany 和 Regex.Split clr
【发布时间】:2013-11-24 17:04:21
【问题描述】:

我有一个字符串列表,每个字符串都需要用正则表达式分割,而不是保存在同一个列表中。

List<string> a = new List<string>();
a.Add("big string with a lot of words");
a=a.SelectMany(item=> Regex.Split("\r\n",item)).ToList();

我只是想确保这不会对已创建的字符串部分重新排序?

是否有网站提供有关方法运行时和编译器优化的信息?

谢谢

【问题讨论】:

    标签: .net regex linq c#-4.0 clr4.0


    【解决方案1】:

    使用 Linq-to-Objects,它不会重新排序结果集的元素,除非您调用 OrderBy 或 OrderByDescending。

    但是,我不会为此使用正则表达式,一个简单的string.Split 就可以了:

    List<string> a = new List<string>();
    a.Add("big string with a lot of words");
    a = a.SelectMany(item => item.Split(new[] { "\r\n" }, StringSplitOptions.None)).ToList();
    

    【讨论】:

    • 我只是在这里简化了代码,实际上我使用的是不同的正则表达式....您认为如果在此代码之后我将打印列表中的字符串,我将始终得到我开始使用的字符串?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多