Splitting a csv string file into a multidemensional array

P

Perseus

Hi

To split a single string we do the following
#
myString= "bannana, bowling balls, juice"
string singleArray[]= myString.split(',');
#
This gives
singleArray[1] = "bannana" etc.

But to split a string into a multidemensional array is more complex.
The Arraylist() method has been shown but how do you do it using
generics or other manner?

myString= "bannana, box, bowling balls, crate, juice, bottle"

I want to split this up where each element goes to row and column
alternating to give a 2 dimensional array:
Bannana, Box
Bowling balls, crate
juice, bottle
teeth, mouth

i.e. MultiArray[2,0] = "teeth" and MultiArray[2,1] = "mouth"
Any ideas?

Thanks in advance
 
R

Rob Whiteside

Hi

To split a single string we do the following
#
myString= "bannana, bowling balls, juice"
string singleArray[]= myString.split(',');
#
This gives
singleArray[1] = "bannana" etc.

But to split a string into a multidemensional array is more complex.
The Arraylist() method has been shown but how do you do it using
generics or other manner?

myString= "bannana, box, bowling balls, crate, juice, bottle"

I want to split this up where each element goes to row and column
alternating to give a 2 dimensional array:
Bannana, Box
Bowling balls, crate
juice, bottle
teeth, mouth

i.e. MultiArray[2,0] = "teeth" and MultiArray[2,1] = "mouth"
Any ideas?

Thanks in advance

Could you just split on the newline giving a string for each line,
then iterate through them and split on the comma?

string [] lines = myString.Split("\n");
foreach(stirng line in lines) {
2dArray[i++] = line.split(',');
}

Just my first thought... (I realize there is stuff missing from that
code)

Cheers,
--Rob W
 
S

sherifffruitfly

Hi

To split a single string we do the following
#
myString= "bannana, bowling balls, juice"
string singleArray[]= myString.split(',');

What happens when a data element spans multiple lines? What happens
when a data element itself contains commas, and (hence) is quoted?

Both of these are valid csv possibilities.

cdj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top