将文本文件拆分为数组

时间:2014-12-26 08:54:55

标签: c# arrays text-files

我不知道如何解决这个问题,所以我希望有人可以提供帮助。我有一个文本文件,格式如下:

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

等等......

我希望能够将此文本文件拆分为数组,包含每个包含数字的日期,直到下一个日期

有人能指出我正确的方向吗?一如往常,任何帮助表示赞赏 - 非常感谢和节日快乐

3 个答案:

答案 0 :(得分:3)

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);
        }
    }
}

答案 1 :(得分:1)

以下是您的要求的示例示例。但是我需要硬编码你需要动态创建的数组。

 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());
        }
    }

答案 2 :(得分:0)

为什么不使用REGEX

这是一个示例代码

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();
        }
    }
}