How to split an array into multiple arrays






4.78/5 (6 votes)
How to split an array into different array of size n
Introduction
This tip will help you split an array of size m into multiple arrays of size n.
Using the code
String[] arrayString = new string[]{"","","","",""};
List<string[]> splitted = new List<string[]>();//This list will contain all the splitted arrays.
int lengthToSplit = 3;
int arrayLength = arrayString.Length;
for (int i = 0; i < arrayLength; i = i + lengthToSplit)
{
string[] val = new string[lengthToSplit];
if (arrayLength < i + lengthToSplit)
{
lengthToSplit = arrayLength - i;
}
Array.Copy(arrayString, i, val, 0, lengthToSplit);
splitted.Add(val);
}