-2

How do I take an string input and make it into an int array?

string input = Console.ReadLine();
int numb = Convert.Toint32(input);          
int[] intArray = // what do i write here to make it take the "input" length, and put the input into an int array?
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Daniel
  • 1
  • Can you give example of input string and what the output should be for that string? – Floremin Apr 15 '13 at 21:22
  • possible duplicate of [Convert comma separated string of ints to int array](http://stackoverflow.com/questions/1763613/convert-comma-separated-string-of-ints-to-int-array) – Conrad Frix Apr 15 '13 at 21:53

4 Answers4

2

You don't give much detail, but if the input is a comma-delimited list of numbers you could do:

string input = "1,2, 3,4  ,5 ,6";  // string to simulate input
int[] numbers = input.Split(new char[] {','})
                     .Select(s => int.Parse(s))
                     .ToArray();

this will obviously blow up if any string between commas is not a valid integer.

D Stanley
  • 139,271
  • 11
  • 154
  • 219
0

You can get an array from a string in two different ways.

You can either use the split method, which breaks the string into an array of substrings - and then you'd have to parse each element of this array. This is probably what you want, since it seems you want an array of integers;

Or you can convert the string to a byte array. Means to do so have been discussed in this question. Then you convert those values to integers as you see fit.

Community
  • 1
  • 1
Geeky Guy
  • 8,658
  • 4
  • 38
  • 60
0
string input = Console.ReadLine();
int numb = Convert.ToInt32(input);
int[] intArray = new int[numb];
for (int i; i < intArray.length; i++)
{
   intArray[i] = numb;
}
BrOSs
  • 899
  • 4
  • 10
  • 26
0

For obtaining the length of the input you can do the following:

    string input = Console.ReadLine();
    int numb = input.Length;
    int[] intArray = new int[1];
    intArray[0] = numb;
devilfish17
  • 337
  • 3
  • 10