Frage

I want to seperate txt data by " " (space character) with c# code. Data is seen in the below. I want there to be columns and reach all the columns seperately. For example copying 3rd and 4th columns of 3rd, 4th and 5th rows. A lot of codes are tried but not working. I searched the web and i can't run the codes that i found. How can i do that?

data inside the text.txt file

1
41
973 3.93E-03 9.99E-02 2.00E+01 3.90E-01 0.00E+00 0.00E+00
975 7.85E-03 9.97E-02 2.00E+01 3.90E-01 0.00E+00 0.00E+00
977 1.18E-02 9.93E-02 2.00E+01 3.90E-01 0.00E+00 0.00E+00
979 1.56E-02 9.88E-02 2.00E+01 3.89E-01 0.00E+00 0.00E+00
981 1.95E-02 9.81E-02 2.00E+01 3.88E-01 0.00E+00 0.00E+00
983 2.33E-02 9.72E-02 2.00E+01 3.87E-01 0.00E+00 0.00E+00

War es hilfreich?

Lösung

Try:

var fileContent = File.ReadAllLines("data.txt");
var separated = fileContent.Select(line => line.Split(' ').ToArray();

You'll end up with array (rows) of values (columns). To access them use:

var row4Col5 = separated[3][4];

Not that you might want to do some boundaries checks, as some lines of your data file don't contain all columns.

Andere Tipps

You can easily read the whole text file into an array of strings:

string[] content = File.ReadAllLines("{path to your file");

And then skip the first 2 lines which are not relevant in this case and divide each line into an array of values:

string[][] array = content.Skip(2).Select(line => line.Split(' ')).ToArray();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top