If you know that the entire length of the string is exactly divided by the size of the part, use:
var whole = "32427237!"; var partSize = 3; var parts = Enumerable.Range(0, whole.Length / partSize) .Select(i => whole.Substring(i * partSize, partSize));
But if there is a possibility that the whole line may have a fractional fragment at the end, you need a little more refinement:
var whole = "32427237"; var partSize = 3; var parts = Enumerable.Range(0, (whole.Length + partSize - 1) / partSize) .Select(i => whole.Substring(i * partSize, Math.Min(whole.Length - i * partSize, partSize)));
In these examples, the parts will be IEnumerable, but you can add .ToArray () or .ToList () at the end if you need a string [] or List <string> value.
Dave Lampert Jul 03 '16 at 16:44 2016-07-03 16:44
source share