string to integer array

H

Howard

How would I convert a string to an integer arrary or arraylist?


example:

string mystring = "1, 2, 3";
//convert to
int[] myArr = {1, 2, 3};


Thanks in advance,

Howard
 
L

Lebesgue

public static int[] StringToInts(string myString)
{
List<int> ints = new List<int>();
string[] strings = myString.Split(',');

foreach (string s in strings)
{
int i;
if (int.TryParse(s.Trim(), out i))
{
ints.Add(i);
}
}
return ints.ToArray();
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

There is no "one method call" way of doing this. Split the string , create
the array and convert each string piece to int
 
G

Guest

public static int[] StringToInts(string myString)
{
List<int> ints = new List<int>();
string[] strings = myString.Split(',');

foreach (string s in strings)
{
int i;
if (int.TryParse(s.Trim(), out i))
{
ints.Add(i);
}
}
return ints.ToArray();
}

Like that, but I wouldn't use the List (why use a generic and then hard-code
it to use only one type?), I'd do something like:

string[] strings = myString.Split(',') ;
int[] ints = new int [ strings.Length ] ;

for ( int i = 0 ; i < string.Length ; i++ )
{
ints [ i ] = int.Parse ( strings [ i ] ) ; // Provide some protection
here
}

return ( ints ) ;

I would probably also provide for a list of seperators to be passed in,
rather than having the comma hard-coded.

The generic implementation would be good if you wanted a class that could
split the string and provide a list of values of some provided type. I'll
leave that as an exercise :)
 
L

Lebesgue

No, using generic List with a "hardcoded" type is absolutely OK and common
practice. It's just like using an ArrayList.

What if your "// Provide some protection here" "failed"? You would return
zeroes instead of the bad (non int) values, which does not comply to the
specification provided. That's why you need to have a "variable size array"
(List<int>) instead of int[] here.


PIEBALD said:
public static int[] StringToInts(string myString)
{
List<int> ints = new List<int>();
string[] strings = myString.Split(',');

foreach (string s in strings)
{
int i;
if (int.TryParse(s.Trim(), out i))
{
ints.Add(i);
}
}
return ints.ToArray();
}

Like that, but I wouldn't use the List (why use a generic and then
hard-code
it to use only one type?), I'd do something like:

string[] strings = myString.Split(',') ;
int[] ints = new int [ strings.Length ] ;

for ( int i = 0 ; i < string.Length ; i++ )
{
ints [ i ] = int.Parse ( strings [ i ] ) ; // Provide some protection
here
}

return ( ints ) ;

I would probably also provide for a list of seperators to be passed in,
rather than having the comma hard-coded.

The generic implementation would be good if you wanted a class that could
split the string and provide a list of values of some provided type. I'll
leave that as an exercise :)
 
G

Guest

What if your "// Provide some protection here" "failed"? You would return

I meant the .Parse (or .TryParse as you used) needed protection that I was
too lazy to write. I would have a try/catch that would throw on invalid data.
I don't think the user would want to put in three values and get back two
without knowing that one failed and which one.

But that's up to the implementer, maybe provide another parameter that
controls what to do with invalid data -- throw or ignore. Or provide for
passing a delegate to the error handling?
 

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

Top