Splitting a string into chunks of a certain size

后端 未结 30 1602
时光说笑
时光说笑 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:12

    It's not pretty and it's not fast, but it works, it's a one-liner and it's LINQy:

    List a = text.Select((c, i) => new { Char = c, Index = i }).GroupBy(o => o.Index / 4).Select(g => new String(g.Select(o => o.Char).ToArray())).ToList();
    

提交回复
热议问题