Search code examples
c#.netfilestringstring-parsing

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


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


Solution

  • 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();