I have a txt file with the following data:
Code |
I'd like to read that file, remove the header, remove col1 ......col4.
So, at the end I'd like to have the following data in a two-dimensional array so that I can manipulate (copy, sort...) them:
Code |
I've found on this and other forums how to read the txt, but no solutions how to remove all these unwanted lines.
I'm reading the file so:
Code |
Thanks for your help
Kabanga

Zoran HorvatRank #792 · 2.9K reputationPosted Nov 16, 2010, 4:28 AM EST
Here is the code which does that - type lines and finish with a line consisting of single dash (-):
class Program
{
static void Main(string[] args)
{
List list = new List();
while (true)
{
string line = Console.ReadLine();
if (line == "-")
break;
else list.Add(line);
}
string[] lines = list.ToArray();
string[][] cells = new string[lines.Length][];
for (int i = 0; i < cells.Length; i++)
cells[i] = lines[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("------------------------------------");
for (int i = 0; i < cells.Length; i++)
{
for (int j = 0; j < cells[i].Length; j++)
Console.Write("[{0}] ", cells[i][j]);
Console.WriteLine();
}
Console.WriteLine("------------------------------------");
}
}
Input:
------------------------------------
[line1]
[line2]
[line3]
[col1] [col2] [col3] [col4]
[1] [3] [12] [63]
[83] [10] [19] [14]
[21] [34] [87] [54]
------------------------------------
You can skip headers printing by starting the printout loop from specified index (e.g. 1 instead of 0).
Jean PaulRank #32 · 38.5K reputationPosted Nov 16, 2010, 4:27 AM EST
and discard the lines containing alphabets
Again you can split based on space and get the values
private void Form1_Load(object sender, EventArgs e) { string text = new StreamReader("YourFile.txt").ReadToEnd(); int[,] result = CreateArray(text); } private int[,] CreateArray(string data) { int[,] array = new int[10, 10]; int i = 0, j = 0; foreach (string line in data.Split(new string[1] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) { if ((IsNumeric(line) && (line.Trim().Length > 0))) // If valid numeric line and not empty line { j = 0; foreach (string number in line.Split(' ')) if (!string.IsNullOrEmpty(number)) array[i, j++] = int.Parse(number); i++; } } return array; } private bool IsNumeric(string line) { foreach(char c in line) if (!"0123456789 ".Contains(c)) return false; return true; }Sam HobbsRank #107 · 20.5K reputationPosted Nov 17, 2010, 12:13 AM EST
Jo3 reputationPosted Nov 17, 2010, 1:06 AM EST
Using the method of Zoran I could read the data into a 2d array.
Sam, I like your idea. Combining both Zoran's and your idea I'd get exactly what I want.
What I'm doing now is manually counting the lines till the beginning of numeric data.
With that method, if the number of lines changes, I must manually change the code to adapt to the new situation.
Before the beginning of numeric data in the txt file there is always the string " End Comments".
I'm wondering if there is any function like "ScanLine( path, stringToFind)" that I'll use for detecting the string " End Comments".
Best regards