C# - How to parse text file (space delimited numbers)?

q0987 picture q0987 · May 20, 2011 · Viewed 11.8k times · Source

Given a data file delimited by space,

10 10 10 10 222 331 
2 3 3 4 45
4 2 2 4

How to read this file and load into an Array

Thank you

Answer

Alex Aza picture Alex Aza · May 20, 2011
var fileContent = File.ReadAllText(fileName);
var array = fileContent.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

if you have numbers only and need a list of int as a result, you can do this:

var numbers = array.Select(arg => int.Parse(arg)).ToList();