222
votes

I have a string that has numbers

string sNumbers = "1,2,3,4,5";

I can split it then convert it to List<int>

sNumbers.Split( new[] { ',' } ).ToList<int>();

How can I convert string array to integer list? So that I'll be able to convert string[] to IEnumerable

11
We had exactly the same question today: Click me - Dario
in "one line" si a very strong is a very strict requirement! </perl> - dfa
This question specifically says to split a string of numbers, which keeps the answer simple. The question Dario mentioned handles (bogs down in?) issues of TryParse for general strings. - goodeye

11 Answers

540
votes
var numbers = sNumbers.Split(',').Select(Int32.Parse).ToList();
36
votes

You can also do it this way without the need of Linq:

List<int> numbers = new List<int>( Array.ConvertAll(sNumbers.Split(','), int.Parse) );

// Uses Linq
var numbers = Array.ConvertAll(sNumbers.Split(','), int.Parse).ToList();
32
votes

Better use int.TryParse to avoid exceptions;

var numbers = sNumbers
            .Split(',')
            .Where(x => int.TryParse(x, out _))
            .Select(int.Parse)
            .ToList();
19
votes

Joze's way also need LINQ, ToList() is in System.Linq namespace.

You can convert Array to List without Linq by passing the array to List constructor:

List<int> numbers = new List<int>( Array.ConvertAll(sNumbers.Split(','), int.Parse) );
11
votes

It is also possible to int array to direct assign value.

like this

int[] numbers = sNumbers.Split(',').Select(Int32.Parse).ToArray();
5
votes

You can use new C# 6.0 Language Features:

  • replace delegate (s) => { return Convert.ToInt32(s); } with corresponding method group Convert.ToInt32
  • replace redundant constructor call: new Converter<string, int>(Convert.ToInt32) with: Convert.ToInt32

The result will be:

var intList = new List<int>(Array.ConvertAll(sNumbers.Split(','), Convert.ToInt32));
4
votes

also you can use this Extension method

public static List<int> SplitToIntList(this string list, char separator = ',')
{
    return list.Split(separator).Select(Int32.Parse).ToList();
}

usage:

var numberListString = "1, 2, 3, 4";
List<int> numberList = numberListString.SplitToIntList(',');
3
votes

On Unity3d, int.Parse doesn't work well. So I use like bellow.

List<int> intList = new List<int>( Array.ConvertAll(sNumbers.Split(','),
 new Converter<string, int>((s)=>{return Convert.ToInt32(s);}) ) );

Hope this help for Unity3d Users.

2
votes

My problem was similar but with the inconvenience that sometimes the string contains letters (sometimes empty).

string sNumbers = "1,2,hh,3,4,x,5";

Trying to follow Pcode Xonos Extension Method:

public static List<int> SplitToIntList(this string list, char separator = ',')
{
      int result = 0;
      return (from s in list.Split(',')
              let isint = int.TryParse(s, out result)
              let val = result
              where isint
              select val).ToList(); 
}
0
votes

Why stick with just int when we have generics? What about an extension method like :

    public static List<T> Split<T>(this string @this, char separator, out bool AllConverted)
    {
        List<T> returnVals = new List<T>();
        AllConverted = true;
        var itens = @this.Split(separator);
        foreach (var item in itens)
        {
            try
            {
                returnVals.Add((T)Convert.ChangeType(item, typeof(T)));
            }
            catch { AllConverted = false; }
        }
        return returnVals;
    }
-2
votes

You can use this:

List<Int32> sNumberslst = sNumbers.Split(',').ConvertIntoIntList();