3 Replies - 339 Views - Last Post: 10 October 2011 - 02:15 AM Rate Topic: -----

#1 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Integer Arrays

Posted 09 October 2011 - 09:08 AM

Is there any method for ints equivalent to "ToCharArray"?

I need to enter a number (for instance, 5532) and I want to put it into an int array, like each part of the number is a separate element.


The only thing I can think to do is to use a loop to put each number into the array, but I can't think of how to do that.
Is This A Good Question/Topic? 0
  • +

Replies To: Integer Arrays

#2 CodingSup3rnatur@l-360  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: Integer Arrays

Posted 09 October 2011 - 09:28 AM

Hi,

There is no method in the framework that will do that directly. However, you would do it with a loop (or use LINQ).

You can treat the string as an array of characters, meaning you can loop through it as such, accessing elements using an index. So, you first need to create an int array that has the same length of the string the user entered (use the Length property of the string class to do this). You then need to loop through the entered string (I would use a for loop), trying to parse each character to an integer (for which I would use int.TryParse(). As you parse each character to an int, assign it to the corresponding position in your int array :)
Was This Post Helpful? 2
  • +
  • -

#3 s3thst4  Icon User is offline

  • a * a = (b * b) + (c * c) - 2(b)(c)cos(A)
  • member icon

Reputation: 10
  • View blog
  • Posts: 587
  • Joined: 20-November 08

Re: Integer Arrays

Posted 09 October 2011 - 09:39 AM

Ahhh! I appreciate it so much! Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Integer Arrays

Posted 10 October 2011 - 02:15 AM

An example

  static void Main(string[] args)
        {
            int []iArr = StringToIntConverter("55344654",true);
        }
        public static int[] StringToIntConverter(string strData,bool Reverse)
        { 
            int[] iArray = new int[strData.Length];

            if (!Reverse) for (int i = 0; i < strData.Length; i++)
                    iArray[i] = int.Parse(strData[i].ToString());

            else for (int i = 0; i < strData.Length; i++)
                iArray[i] = int.Parse(strData[(strData.Length - 1) - i].ToString());

            return iArray;
        }


This post has been edited by Curtis Rutland: 11 October 2011 - 08:53 AM
Reason for edit:: removed size tags from unreadable code

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1