Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old January 12th, 2007, 08:17 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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>
  #2 (permalink)  
Old January 12th, 2007, 09:06 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #3 (permalink)  
Old January 12th, 2007, 05:52 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #4 (permalink)  
Old January 12th, 2007, 11:58 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #5 (permalink)  
Old May 2nd, 2010, 06:07 PM
Registered User
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
  #6 (permalink)  
Old November 16th, 2010, 08:58 PM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
  #7 (permalink)  
Old November 17th, 2010, 04:24 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #8 (permalink)  
Old November 17th, 2010, 10:47 AM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #9 (permalink)  
Old November 17th, 2010, 11:57 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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));
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #10 (permalink)  
Old November 17th, 2010, 12:02 PM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
String to array ajit Javascript 5 January 20th, 2007 04:02 PM
String to Array Psion PHP How-To 1 September 5th, 2006 11:36 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Instantiate Integer array in a stucture hashtlishnii VB.NET 2002/2003 Basics 2 June 18th, 2004 11:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.