-3

I have lines (string type) of numbers like "23 78 53 4 94 32 148 31". I need to put them into int array. Here's the lame code I wrote, but it doesn't work properly:

        int currentArrayValue=0;
        int currentChar=0;

        for (int i = 0; i < text1.Length; i++)
        {
            if (text1[i] != ' ')
            {
                currentChar++;
                continue;
            }

            else
            {
                for (int k = 1; k <=currentChar; k++)
                {
                    if (k == 1) Array[currentArrayValue] = text1[i - currentChar];
                    else if (k == 2) Array[currentArrayValue] = text1[i - currentChar] * 10;
                    else if (k == 3) Array[currentArrayValue] = text1[i - currentChar] * 100;
                }
                currentArrayValue++;
            }
        }

It can be a one-, two- or three-digit number.

Bat Man
  • 35
  • 3
  • 2
    `it doesn't work properly` is not a helpful description of the problem. What do you expect to get? What did you get instead? Are you getting an error/exception? If so, what is and where is it happening? *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* – tnw Oct 13 '15 at 18:02
  • can you not just split by spaces and then `Convert.ToInt32()` on each one. You can either use a list or just allocate a new array of length, `splitStringArray.Length` – Cjen1 Oct 13 '15 at 18:23

5 Answers5

2

There are several ways to achieve what you want as the other answers point out. If you want a bit of safety checking then I'd use TryParse:

using System;
using System.Collections.Generic;               
using System.Linq;

public class Program
{
    public static void Main()
    {
        var test = "1 2 3 x";
        var values = test.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries);
        var results = new List<int>();
        foreach(var value in values)
        {
            int x;
            if(Int32.TryParse(value, out x))
            {
               results.Add(x);
            }
            else
            { 
                Console.WriteLine("{0} is not an integer", value);
            }
        }
    }
}
venerik
  • 5,766
  • 2
  • 33
  • 43
1

So if you have a string as such and all the numbers are separated by spaces

string foo = "12 34 567 120 12";

All you have to do is use string.Split(' ') to break this string into an array then convert those values to int values.

int[] intArray = foo.Split(' ').Select(x => Convert.ToInt32(x)).ToArray();
Jonathan Carroll
  • 880
  • 1
  • 5
  • 20
  • For safety, `Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries)` - still works if there are extra spaces. – Arghya C Oct 13 '15 at 18:15
0

Use string.Split instead. You can split on each space and get an array. From there, parse each item to an int. You can actually do it all in one line with LINQ.

int[] myInts = text1.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries)
    .Select(s => int.Parse(s)).ToArray(); 
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
0
string input = "23 78 53 4 94 32 148 31";
var arr = Regex.Matches(input, @"\d+").Cast<Match>().Select(m => int.Parse(m.Value)).ToArray();
Eser
  • 12,346
  • 1
  • 22
  • 32
0

Non-Linq answer

const string inputVal = "23 78 53 4 94 32 148 31";
string[] splitInput = inputVal.Split(' ');
int[] inputIntArray = new int[splitInput.Length];
int counter = 0;
foreach (string split in splitInput){
    inputIntArray[counter++] = int.Parse(split);
}
AWinkle
  • 673
  • 8
  • 18