473,508 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string to integer array

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
May 18 '06 #1
5 39601
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();
}

"Howard" <ho*******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP03.phx.gbl...
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

May 18 '06 #2
Hi,

There is no "one method call" way of doing this. Split the string , create
the array and convert each string piece to int
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Howard" <ho*******@yahoo.com> wrote in message
news:es**************@TK2MSFTNGP03.phx.gbl...
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

May 18 '06 #3
> 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 :)

May 18 '06 #4
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" <PI*****@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.com...
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 :)

May 18 '06 #5
> 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?
May 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6092
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something...
1
3099
by: Darsin | last post by:
Hi all, I am a new programmer to C# and i am having a following problem. I want to make a single method which takes a variable length array and display it contents. i have defined the method as:...
10
5202
by: Peter Stojkovic | last post by:
I want store an integer-array of 1000 Values in a blob in a SQL-database. I will do this every 10 Seconds. How can I do this ???? What datatypes a have to use ??? Thanks
1
2185
by: Sivaraman.S | last post by:
Hi, Can i pass integer array to methodInfo.Invoke(obj,args()). I am able to pass only string array to this function. This is the code i have written. Dim myType As Type = objClass.GetType()...
16
226568
by: kujahleague | last post by:
Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character,...
1
1231
by: Nikolay Petrov | last post by:
What is the best method to store an Integer array in MS Access DB example class testClass public ID1 as integer public ID2 as integer public Data() as Integer End Class
7
2712
by: rhitz1218 | last post by:
Hi all: Is it possible to store an integer array to another integer array? Or should I use an array of pointers that will point to a particular array? Thanks for the help.
2
2138
by: quietforever | last post by:
How do you convert a character array into a integer array? For example: If the character array is entered as'12' then how can get an integer array that contains the integers 1 and 2?
2
4528
by: rsk | last post by:
Friends, The following logic just converts the hexadecimal values to the binay values taken into the array. for(i=31;i>=0;i--){ if ((1<<i)&var1) array_var1=1; else array_var1=0;
3
9092
by: Jerry West | last post by:
I'd like to get the upper bound index of an integer array. I've tried the following: Dim i as Integer Dim arrayIng() as Integer i = arrayIng.GetUpperBound This doesn't work. It seems...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.