Loop through comma delimited string, split into multiple arrays?

26,949

Solution 1

I'm not sure exactly what you're looking for, but to turn the above into three separate arrays, I'd do something like:

var primeArray = yourString.Split(,);
List<string[]> arrays = new List<string[]>();
for(int i = 0; i < primeArray.Length; i += 3)
{
  var first = primeArray[i];
  var second = primeArray[i+1];
  var third = primeArray[i+2];

  arrays.Add(new string[] {first, second, third});
}

Then you can iterate through your list of string arrays and do whatever.

This does assume that all of your string arrays will always be three strings long- if not, you'll need to do a foreach on that primeArray and marshal your arrays more manually.

Here's the exact code I used. Note that it doesn't really change anything from my original non-compiled version:

var stringToSplit = "div, v6571, 0, div, v8173, 300, p, v1832, 400";
List<string[]> arrays = new List<string[]>();
var primeArray = stringToSplit.Split(',');
for (int i = 0; i < primeArray.Length; i += 3)
{
   var first = primeArray[i];
   var second = primeArray[i + 1];
   var third = primeArray[i + 2];
   arrays.Add(new string[] { first, second, third });
}

When I check this in debug, it does have all three expected arrays.

Solution 2

.Split(",") is your best bet. You can then modify that string array to reflect whatever structure you need.

You could use Regular Expressions or other methods, but nothing will have the performance of String.Split for this usage case.

Solution 3

Just so that I'm understanding you right, you need to sort them into:

1) character only array 2) character and number 3) numbers only

If so, you can do the following:

1) First try to parse the string with Int32.Parse if successful store in the numbers array 2) Catch the exception and do a regex for the numbers to sort into the remainder 2 arrays

Hope it helps (: Cheers!

Solution 4

The following assumes that your array's length is a multiple of three:

var values = str.Split(',')

string[,] result = new string[values .Length / 3, 3];
for(int i = 0; i < params.Length; i += 3)
{
    int rowIndex = i / 3;
    result[rowIndex, 0] = values [i];
    result[rowIndex, 1] = values [i + 1];
    result[rowIndex, 2] = values [i + 2];
}

Compiled in my head, but it should work.

Share:
26,949
dmackerman
Author by

dmackerman

Design and UI Engineering at Kentik.

Updated on May 05, 2020

Comments

  • dmackerman
    dmackerman about 4 years

    I have a string coming in the format:

    div, v6571, 0, div, v8173, 300, p, v1832, 400
    

    I want to split this string into multiple arrays, for the example above I would need 3 arrays, such that the format would be like this:

    item[0] -> div
    item[1] -> v6571
    item[2] -> 0
    

    I know that I can just do a .Split(',') on the string and put it into an array, but that's one big array. For the string example above I would need 3 arrays with the structure provided above. Just getting a bit confused on the iteration over the string!

    Thanks!

  • dmackerman
    dmackerman about 13 years
    This seems to work, but it's not adding the last array (the group of 3) to the main array. It's only adding the first 2.
  • AllenG
    AllenG about 13 years
    Have you walked it through debug? What happens?
  • AllenG
    AllenG about 13 years
    When I ran through on my machine, it worked- if you want, I can post the exact code that I compiled.
  • brianc
    brianc over 3 years
    Typo up top. Split(,) should be Split(',')