How to split an array into multiple arrays

This tip will help you split an array of size m into multiple arrays of size n.

 

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);
}
Advertisements
By Sriramjithendra Posted in C#.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s