Monday, August 01, 2011

C#: Convert a string having comma separated integers to an array of integers

Today I saw a person on forums asking this "How to convert a string having comma separated integers to an array of integers" . He was having a string in this format "1,4,6,10,15,20" and he wanted to convert it into an array of integers. I decided to wrie a blog on this for converting the string to arrayof integers. Below is the code:

using System.Linq;

namespace MyConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string myString = "1,4,6,10,15,20";

            int[] output = myString.Split(',')
                .Select(x => int.Parse(x)).ToArray();
           
            //Do your work with output
        }
    }
}


Read more: codeasp.net
QR: c-convert-a-string-having-comma-separated-integers-to-an-array-of-integers

Posted via email from Jasper-Net