字符串分割到使用LINQ的N-长度字符串列表 [英] Split string into list of N-length strings using LINQ

查看:334
本文介绍了字符串分割到使用LINQ的N-长度字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道String.Split的概念已经用不同的方法众多前处理,但我在LINQ解决这个问题特别感兴趣。



我试图写一个扩展类来处理拆分,但两者都尝试一些重大问题。因此,对于以下内容:

 字符串s =ABCDEFGHIJKLMNOPQRSTUVWX; 
变种结果= s.SplitEvery(4);



我想一个列表,如:
{ABCD,EFGH, IJKL,MNOP,QRST,UVWX}



下面是我的扩展类:

 公共静态类扩展
{
公共静态列表<串GT; SplitEvery(这个字符串s,诠释N)
{
名单,LT;字符串>名单=新名单,LT;串>();

VAR Attempt1 = s.Select((C,I)= I标记%N == 0 s.Substring(I,N):|)。其中(x => !X =|)了ToList();

变种ATTEMPT2 = s.Where((C,I)= I标记%正== 0)。选择((C,I)=> s.Substring(I,N)) .ToList();

返回列表;
}
}



1尝试插入一个虚拟字符串|每次条件不满足的时候,然后删除虚拟字符串的所有实例创建的最终名单。它的工作原理,但创建坏串似乎是不必要的额外步骤。此外,如果该字符串不是均匀地用n整除此尝试失败。



尝试二是我想只选择子,其中指数为除尽N,但我在Select语句值不对应于Where语句'我'的价值,所以我得到这样的结果:{ABCD,BCDE,等等...}



我觉得我接近一个很好的解决方案,但可能会在正确的方向上使用一个有用的微调。有什么建议?





我结束了一个组合去建议,以处理我的字符串分离器。它可能不是最快的,但作为一个新手LINQ,这个实现是最简洁,容易让我明白了。

 公共静态列表<串GT; SplitEvery(这个字符串s,诠释大小)
{
返回s.Select((X,I)= I标记)
。凡(I = I标记%大小== 0 )
。选择(I => String.Concat(s.Skip(I)。取(大小)))了ToList();
}



感谢所有的很好的建议。


< DIV CLASS =h2_lin>解决方案

下面是另一种解决方案:

  VAR的结果= S。选择((X,ⅰ)= I标记)
。凡(ⅰ= I标记%4 == 0)
。选择(ⅰ= GT; s.Substring(ⅰ,s.Length - I> = 4?4:s.Length - 我));


I know the concept of String.Split has been addressed before with a multitude of different approaches, but I am specifically interested in a LINQ solution to this question.

I've attempted to write an extension class to handle the split, but both attempts have some major issues. So for the following:

string s = "ABCDEFGHIJKLMNOPQRSTUVWX";
var results = s.SplitEvery(4);

I would want a list like: { "ABCD", "EFGH", "IJKL", "MNOP", "QRST", "UVWX" }

Here is my extension class:

public static class Extensions
{
    public static List<string> SplitEvery(this string s, int n)
    {
        List<string> list = new List<string>();

        var Attempt1 = s.Select((c, i) => i % n== 0 ? s.Substring(i, n) : "|").Where(x => x != "|").ToList();

        var Attempt2 = s.Where((c, i) => i % n== 0).Select((c, i) => s.Substring(i, n)).ToList();

        return list;
    }
}

Attempt 1 inserts a dummy string "|" every time the condition isn't met, then removes all instances of the dummy string to create the final list. It works, but creating the bad strings seems like an unnecessary extra step. Furthermore, this attempt fails if the string isn't evenly divisible by n.

Attempt 2 was me trying to select only substrings where the index was divisible by N, but the 'i' value in the Select statement doesn't correspond to the 'i' value in the Where statement, so I get results like: { "ABCD", "BCDE", etc... }

I feel like I'm close to a good solution, but could use a helpful nudge in the right direction. Any suggestions?

[Edit]

I ended up going with a combination of suggestions to handle my string-splitter. It might not be the fastest, but as a newbie to LINQ, this implementation was the most succinct and easy for me to understand.

public static List<string> SplitEvery(this string s, int size)
{
    return s.Select((x, i) => i)
        .Where(i => i % size == 0)
        .Select(i => String.Concat(s.Skip(i).Take(size))).ToList();
}

Thanks for all the excellent suggestions.

解决方案

Here is another solution:

var result = s.Select((x, i) => i)
              .Where(i => i % 4 == 0)
              .Select(i => s.Substring(i, s.Length - i >= 4 ? 4 : s.Length - i));

这篇关于字符串分割到使用LINQ的N-长度字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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