 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 12th, 2007, 08:17 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Convering a String Array to an Integer array
Hi,
Is it possible to convert a string array which basically has no.'s to an integer array .. if so how ..?
------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
__________________
------------------------------------------------------------------------------------------------------------
<b><i>Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic </i></b>
|
|

January 12th, 2007, 09:06 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
for(int i=0;i<arrLen;i++)
str = arInfo[i];
index = Convert.ToInt32(str);
I'm getting the following exception-
Exception Details: System.FormatException: Input string was not in a correct format.
The arInfo[] is basically an array of numbers
------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
|
|

January 12th, 2007, 05:52 PM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Please check this:
string[] arInfo = { "0", "1", "2", "3" };
int[] iarInfo = new int[4];
for (int i = 0; i < arInfo.Length; i++)
{
iarInfo[i] = Convert.ToInt32(arInfo[i]);
Console.Write(iarInfo[i]);
}
i hope it will work for you.
|
|

January 12th, 2007, 11:58 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I suppose that's the code that i used, and it doesn't work.
------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
|
|

May 2nd, 2010, 06:07 PM
|
|
Registered User
|
|
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The code works, just get sure that there is no "space" at the end of the last char in your string, if there is a space, then remove it.
|
|

November 16th, 2010, 08:58 PM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Extension Method Option
I just wanted to let you know that I have created a generic approach to this problem that allows you to specify the type you want the resulting array or list to be. A blog post describing this solution can be found here.
|
|

November 17th, 2010, 04:24 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You use Generics. This is a C# 1.0 forum, therefore I would assume generics weren't allowed.
If they were allowed then just use the List<T>.ConvertAll() method instead.
|
|

November 17th, 2010, 10:47 AM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I do apologize for posting in the incorrect forum. I didn't notice it was a C# 1.0 forum.
You propose and interesting suggestion. The only thing about the ConvertAll method is that it requires you to write a delegate that does the conversion from one type to another. First, that could separate the code more so you have to go looking for the converter logic (then again since we are out of the C# 1.0 realm, you could use an anonymous delegate which would place the code inline). Second, if you really want to make it generic like I did, you still have to use the Convert.ChangeType method or something similar as you can't directly cast a string to a decimal, float, etc and obviously you can throw in a XXX.Parse as that would be binding to a given type.
While ConvertAll is a great use of the framework, is there any real benefit of using it over what I did? Maybe this isn't the greatest place to continue this discussion but I would be interested in your feedback.
Thanks for you comment!
Nick
|
|

November 17th, 2010, 11:57 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I guess it depends on how often you are going to be doing splits. Personally the code you have written to do it the generic way is great, but is much more code to write than simply doing it in line like this:
Code:
string theString = "1,2,3,4,5";
List<int> ints = theString.Split(',').ToList().ConvertAll<int>(s => Convert.ToInt32(s));
|
|

November 17th, 2010, 12:02 PM
|
|
Registered User
|
|
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
True true. Just like everything else in life, its conditional! You are right, if my goal wasn't re-usability and broad application the method you presented is a great one line solution. I think I will amend my post to include the solution you presented, citing you of course.
Thanks again.
Nick
|
|
 |