Split a string into chunks of a certain size in C#
This post will discuss how to split a string into chunks of a certain size in C#. For example, splitting a string AAAAABBBBBCCCCC into chunks of size 5 will result into substrings [AAAAA, BBBBB, CCCCC].
1. Using LINQ
We can use LINQ’s Select() method to split a string into substrings of equal size. The following code example shows how to implement this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections.Generic; using System.Linq; public static class Extensions { public static IEnumerable<string> Split(this string str, int n) { if (String.IsNullOrEmpty(str) || n < 1) { throw new ArgumentException(); } return Enumerable.Range(0, str.Length / n) .Select(i => str.Substring(i * n, n)); } } public class Example { public static void Main() { string str = "AAAAABBBBBCCCCC"; int size = 5; IEnumerable<string> s = str.Split(size); Console.WriteLine(String.Join(Environment.NewLine, s)); } } /* Output: AAAAA BBBBB CCCCC */ |
2. Using String.Substring() method
Another solution is to simply use the String.Substring() method to break the string into substrings of the given size, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections.Generic; public static class Extensions { public static IEnumerable<string> Split(this string str, int n) { if (String.IsNullOrEmpty(str) || n < 1) { throw new ArgumentException(); } for (int i = 0; i < str.Length; i += n) { yield return str.Substring(i, n); } } } public class Example { public static void Main() { string str = "AAAAABBBBBCCCCC"; int size = 5; IEnumerable<string> s = str.Split(size); Console.WriteLine(String.Join(Environment.NewLine, s)); } } /* Output: AAAAA BBBBB CCCCC */ |
The above code will throw an exception when the string length is not a multiple of the given size. The following code example shows how to handle this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System; using System.Collections.Generic; public static class Extensions { public static IEnumerable<string> Split(this string str, int n) { if (String.IsNullOrEmpty(str) || n < 1) { throw new ArgumentException(); } for (int i = 0; i < str.Length; i += n) { yield return str.Substring(i, Math.Min(n, str.Length - i)); } } } public class Example { public static void Main() { string str = "AAAAABBBBBCCCCC"; int size = 5; IEnumerable<string> s = str.Split(size); Console.WriteLine(String.Join(Environment.NewLine, s)); } } /* Output: AAAAA BBBBB CCCCC */ |
3. Using Regex
We can write a regular expression to split a string into substrings of equal size. Here’s the regex one-liner version using LINQ:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Linq; using System.Text.RegularExpressions; using System.Collections.Generic; public static class Extensions { public static IEnumerable<string> Split(this string str, int n) { if (String.IsNullOrEmpty(str) || n < 1) { throw new ArgumentException(); } return from Match `m` in Regex.Matches(str, @"\w{" + n + "}") select m.Value; } } public class Example { public static void Main() { string str = "AAAAABBBBBCCCCC"; int size = 5; IEnumerable<string> s = str.Split(size); Console.WriteLine(String.Join(Environment.NewLine, s)); } } /* Output: AAAAA BBBBB CCCCC */ |
That’s all about splitting a string into chunks of a certain size in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)