posted 7/31/2011 by Raghav Khunger
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 } } }
Above the string is splitted with the separator, comma in this case. Then the values are parsed to integer values and then all the values are put into an array.Output:Do let me know your feedback, comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18