65.9K
CodeProject is changing. Read more.
Home

How to split an array into multiple arrays

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.78/5 (6 votes)

May 30, 2012

CPOL
viewsIcon

111441

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);
}