Im doing a project where i have to read the datas from a .txt file and then insert each line into a table using query.
Now for example the contents of the text file would be
11111
1111x
22222
2222x
33333
3333x
and so on.
Now as you can see that the alternate row is almost repetitive so i would like to remove alternate rows so that the available data becomes
11111
22222
33333
and then process the rest of my codes.
Is there any way i can do that?
So far i have been using the Array list to get this
using (StreamReader sr = new StreamReader(Server.MapPath("03122013114450.txt"), true))
{
string txtValues = sr.ReadToEnd();
string[] txtValuesArray1 = Regex.Split(txtValues, "\r\n");
ArrayList array = new ArrayList();
foreach (string value in txtValuesArray1)
{
array.Add(value);
}
for (int i = 0; i < array.Count; i++)
{
if (array.Count % 2 != 0)
array.RemoveAt(i + 2);
else
array.RemoveAt(i + 1);
}
}
The basic idea is to delete the alternate rows wether it be from the index of the arraylist of from the text file.
Just tried this with LINQ
string[] lines = File.ReadAllLines("your_file_name");
var result = lines.Where((s, idx) => idx % 2 == 0);
of course, if your file is very big, then you need to work line by line and skip the not required lines while reading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With