0

I am using c# to access a text file through file handling. I want to go through all the lines and separate a particular chunk from each line e.g

col1    col2     col3
1949      1       388
1950    2      50

I just want to separate the col3 data and store its contents in an array.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Salman Farooq
  • 159
  • 1
  • 1
  • 11
  • any delimeter there? to rectify that its a column 3? – Sandeep Kushwah Apr 24 '15 at 15:40
  • look up the `string.Split` Method.. this is something that you could have easily found by doing a simple `Google Search` it amazes me how people can find `Stackoverflow` but can seem to find `google.com` – MethodMan Apr 24 '15 at 15:43
  • The search term is [CSV](http://en.wikipedia.org/wiki/Comma-separated_values)/comma separated file. Note that "comma" can be any separator - you should be easily adapt [duplicate](http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-into-an-array) to your needs. – Alexei Levenkov Apr 24 '15 at 15:53

2 Answers2

1

You can do it like this...

var str = @"col1 col2 col3
21312 51245 1235
21311 12 6235";

string[] rows = str.Split('\n')
                   .Select(r => r.Split(' ')[2])
                   .Skip(1)
                   .ToArray();
Aydin
  • 15,016
  • 4
  • 32
  • 42
0

While you are reading file line by line, use Split() method of the string to create string array splitted by your column separator (tab or space) then In your array you created earlier load tempArray[1] whis is your mid value

Yuri
  • 2,820
  • 4
  • 28
  • 40