Splitting A String Into An Array

Hi,

I need help splitting a string into an array

for example:

I want to split a string "11112222333344445555" into an array of 5 values that is "1111", "2222", "3333", "4444", "5555".

I want it to automatically split every 4 characters

Is there a way to do this

Thanks for the help in advance.



Answer this question

Splitting A String Into An Array

  • Mardy

    hi,

    you can try something like this


    static void Main(string[] args)
    {
    string[] newarray = splitByLength("111122223333444455555", 4);
    foreach (string item in newarray)
    {
    Console.WriteLine(item);
    }
    }
    static string[] splitByLength(string target, int chunkLength)
    {
    int total = (int)target.Length / chunkLength;
    string[] myarray = new string[total];
    for (int i = 0; i < total; i++)
    {
    myarray[ i ] = target.Substring((i * chunkLength), chunkLength);
    }
    return myarray;
    }


     



  • James Thorne.

    Thanks alot, I figured it out and got it to work.
  • Splitting A String Into An Array