using System.Collections.Generic;
public static IEnumerable<string> Chunk(this string str, int chunkSize)
throw new ArgumentException("Chunk size cannot be lesser then 0.", nameof(chunkSize));
.Range(0, (int)Math.Ceiling((double)str.Length / chunkSize))
.Select(i => str.Substring(i * chunkSize, Math.Min(chunkSize, str.Length - (i * chunkSize))));
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
if(collection != null && action != null)
foreach(var item in collection)
public static string Implode(this IEnumerable<string> collection, string delimiter)
throw new ArgumentNullException(nameof(collection));
return String.Join(delimiter, collection);
const string TestString = "1234567890abcdefgh";
public static void Main()
TestString.Chunk(4).ForEach(i => Console.WriteLine(i));
Console.WriteLine(TestString.Chunk(5).Implode("-"));