Jay
Jay

Reputation: 97

Split Text File Into Array

I'm not sure how to go about this so I'm hoping someone can be of help. I have a text file in the following format:

2014-12-24:
119:
r2
20
10
r1
24
r31
2014-12-25:
10:
5
r5
r7
2014-12-26:
15:
4
2
4

And so on...

I'd like to be able to split this text file into arrays for each date containing the numbers followed until the next date

Can anyone point me in the right direction? As always, any help is appreciated--thanks a lot and happy holidays

Upvotes: 0

Views: 3563

Answers (3)

Gun
Gun

Reputation: 1411

Below is the sample example for your requirement. But I hard coded arrays you need to create those dynamically.

 static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines(@"D:\test.txt");
        string[] firstArray = new string[7];
        string[] secondArry = new string[4];
        string[] thirdArry = new string[4];
        int i = 0, index = 0;

        foreach (var item in lines)
        {
            var date = item.Trim(new char[] { ':' });
            DateTime dt;

            if (DateTime.TryParse(date, out dt))
            {
                i = 0;
                index = index + 1;
            }
            else
            {
                if (index == 1)
                    firstArray[i] = item.ToString();
                if (index == 2)
                    secondArry[i] = item.ToString();
                if (index == 3)
                    thirdArry[i] = item.ToString();

                i++;
            }
        }

        foreach (var array1 in firstArray)
        {
            Console.WriteLine(array1.ToString());
        }

        foreach (var array2 in secondArry)
        {
            Console.WriteLine(array2.ToString());
        }

        foreach (var array3 in thirdArry)
        {
            Console.WriteLine(array3.ToString());
        }
    }

Upvotes: 1

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13077

Why not use REGEX

Here's an example code

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    using System;
    using System.Text.RegularExpressions;

    class Program
    {
        static void Main()
        {
            string pat = @"\d{4}-\d{2}-\d{2}\:";

            // Instantiate the regular expression object.
            Regex r = new Regex(pat, RegexOptions.IgnoreCase);


            String line;
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\PATH\Filename.txt");
            while ((line = file.ReadLine()) != null)
            {
                               Match m = r.Match(line);
            if (m.Success)
            {
                // A date is found ; make new List/some data structure to extract data on this date
                Console.WriteLine("Date : {0}",m.Value);
            }
            else
            {
                Console.WriteLine("\tVales {0} ",line);
            }
            }

            Console.ReadKey();
        }
    }
}

Upvotes: 0

opewix
opewix

Reputation: 5083

List<string> lines = new List<string>();
List<DateTime> dates = new List<DateTime>();

using (StreamReader sr = new StreamReader(File.OpenRead("Input.txt")))
{
    while (sr.EndOfStream == false) // read all lines in file until end
    {
        string line = sr.ReadLine();
        if(line == null) continue;

        lines.Add(line); // storing line in collection

        // ensuring the line has date, you also may specify date pattern
        DateTime tempDateTime;
        if (DateTime.TryParse(line.Replace(":", ""), out tempDateTime))
        {
            // this line has DateTime
            dates.Add(tempDateTime);
        }
    }
}

Upvotes: 3

Related Questions