Convert Array to string[]

  • Thread starter Thread starter Janaka
  • Start date Start date
J

Janaka

Hi I was wondering if there was an easy way to convert a System.Array object
to a string[] ?

The only solution I can find is to iterate through the System.Array and
populate the string[] object like so:

// populate an Array object called 'values' ....
// create a new string array
string[] newArray = new string[values.Length];

// loop through the 2-D System.Array and populate the 1-D String Array
for (int i = 1; i <= values.Length; i++)
{
if (values.GetValue(1, i) == null)
newArray[i-1] = "";
else
newArray[i-1] = (string)values.GetValue(1, i).ToString();
}
 
Janaka said:
Hi I was wondering if there was an easy way to convert a System.Array object
to a string[] ?

The only solution I can find is to iterate through the System.Array and
populate the string[] object like so:

// populate an Array object called 'values' ....
// create a new string array
string[] newArray = new string[values.Length];

// loop through the 2-D System.Array and populate the 1-D String Array
for (int i = 1; i <= values.Length; i++)
{
if (values.GetValue(1, i) == null)
newArray[i-1] = "";
else
newArray[i-1] = (string)values.GetValue(1, i).ToString();
}

Hmm... what type of array do you have, exactly? Your original question
only talks about a System.Array, but then you're assuming it's a 2D
array, and only getting certain values from it
(and converting null to "").

Note that you'll have only populated part of your new array - it has as
many elements as the original array does *in total*, but you're only
looking at one "row" of the original array.
 
Jon

Ok to simplify the question if I have a 1 dimensional System.Array object is
there a simple way of turning this into a 1 dimensional string[] object?

Jon Skeet said:
Janaka said:
Hi I was wondering if there was an easy way to convert a System.Array object
to a string[] ?

The only solution I can find is to iterate through the System.Array and
populate the string[] object like so:

// populate an Array object called 'values' ....
// create a new string array
string[] newArray = new string[values.Length];

// loop through the 2-D System.Array and populate the 1-D String Array
for (int i = 1; i <= values.Length; i++)
{
if (values.GetValue(1, i) == null)
newArray[i-1] = "";
else
newArray[i-1] = (string)values.GetValue(1, i).ToString();
}

Hmm... what type of array do you have, exactly? Your original question
only talks about a System.Array, but then you're assuming it's a 2D
array, and only getting certain values from it
(and converting null to "").

Note that you'll have only populated part of your new array - it has as
many elements as the original array does *in total*, but you're only
looking at one "row" of the original array.
 
Janaka said:
Ok to simplify the question if I have a 1 dimensional System.Array object is
there a simple way of turning this into a 1 dimensional string[] object?

Then I'd use Array.Copy or Array.CopyTo. However, note that that won't
do the null -> "" conversion that your current code does.
 
Back
Top