dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C# TextBox values to x string array

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Angry C# TextBox values to x string array

    I have a textbox with the following values : 4,5,6,7,8,9,10,11,12,13,15
    I want these values to be split into `x` string arrays with `y` values in each array.

    For example extract into 3 arrays with 4 numbers in each :

    int x = 3;
    string[] batch;

    batch[0] = "4,5,6,7"
    batch[1] = "8,9,10,11"
    batch[2] = "12,13,15"

    For example extract into 5 arrays with 2 numbers in each :
    int x = 5;
    string[] batch;

    batch[0] = "4,5"
    batch[1] = "6,7"
    batch[2] = "8,9"
    batch[3] = "10,11"
    batch[4] = "12,13"
    batch[5] = "15"

    I've tried Regex.split but I can't get it done... Please help

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: C# TextBox values to x string array

    This is easy.

    Just use the string.split method (use comma ',' for split character) and
    split the string into an array of chars, first.

    Then iterate over your array and pick up and resort your characters into other arrays.

  3. #3
    Join Date
    Jan 2010
    Posts
    13

    Re: C# TextBox values to x string array

    You could do something like this (typed in notepad from the head so copy pasting into VS does'nt work)


    string[] splitted = Textbox1.Text.split(',');
    ArrayList al = new ArrayList();
    StringBuilder tempString = new StringBuilder();
    int count = 1;

    foreach (string s in splitted)
    {
    tempString.Append(s + ",");

    if (count == 2)
    {
    al.add(tempString.ToString());
    tempString.Clear() //or something
    count = 0;
    }
    else
    {
    count++;
    }
    }

    Then use al.ToArray() to create the array.
    Last edited by DutchGuy; January 13th, 2010 at 08:54 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)