converting from string to array

D

darrel

well, I've never used arrays and quickly finding that I'm a bit over my
head. I've hit a catch-22 in my understanding of arrays.

I have a comma delimted string that may be of any length:

2, 4, 34

To convert something to an array, it looks like I use the SPLIT command to
populate the array within for/while loop.

But the catch is that to dim an array, it looks like I need to tell it the
number of items I want to store in it to begin with.

So, I'm a bit confused, as SPLIT appears to also make an array. Can I not go
from a split function directly to an array or do I first need to count the
items to dim the array, and then populate it from the split?

-Darrel
 
M

Mark Rae

So, I'm a bit confused, as SPLIT appears to also make an array. Can I not
go
from a split function directly to an array or do I first need to count the
items to dim the array, and then populate it from the split?

Split() does all the work for you - e.g.

string strElements = "1,2,3,4,5,6,7,8";
Array astrElements = strElements.Split(',');
int intArrayLength = astrElements.Length; // returns 8
foreach(string strElement in astrElements)
{
// do something with each element of the array in turn
}
 
D

darrel

Could you not use the array that is returned from Split?

That's what I'm confused about. When I dim an array, don't I need to tell it
how many items it contains? Or does the split function, by default, dim the
array for me?

-Darrel
 
D

darrel

Split() does all the work for you - e.g.
string strElements = "1,2,3,4,5,6,7,8";
Array astrElements = strElements.Split(',');
int intArrayLength = astrElements.Length; // returns 8
foreach(string strElement in astrElements)
{
// do something with each element of the array in turn
}

Ah, OK, so it's definitely not as complicated as I thought. That definitely
makes sense that split would dim the array for me with the applicable amount
of items.

Thanks!

-Darrel
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top