Splitting a string into chunks of a certain size

后端 未结 30 2248
时光说笑
时光说笑 2020-11-22 07:55

Suppose I had a string:

string str = \"1111222233334444\"; 

How can I break this string into chunks of some size?

e.g., breaking t

30条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 08:14

    static List GetChunks(string value, int chunkLength)
    {
        var res = new List();
        int count = (value.Length / chunkLength) + (value.Length % chunkLength > 0 ? 1 : 0);
        Enumerable.Range(0, count).ToList().ForEach(f => res.Add(value.Skip(f * chunkLength).Take(chunkLength).Select(z => z.ToString()).Aggregate((a,b) => a+b)));
        return res;
    }
    

    demo

提交回复
热议问题