2

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!

4
  • How is one supposed to determine the groups of arrays if the same delimiter is used for groups and within groups? Commented May 4, 2011 at 15:28
  • 1
    I'm confused on how your above example is NOT just one array. It certainly looks like one. Commented May 4, 2011 at 15:28
  • @Tejs - It is one array, but he wants it to split to multiple arrays. Commented May 4, 2011 at 15:29
  • Sorry for the confusion. There will always be 3 values for each array. Does that make it more clear? So in the example string above I would need 3 arrays, each with 3 values. Commented May 4, 2011 at 15:29

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Have you walked it through debug? What happens?
When I ran through on my machine, it worked- if you want, I can post the exact code that I compiled.
Typo up top. Split(,) should be Split(',')
4

.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.

Comments

1

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.

Comments

1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.