0

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

2
  • 2
    Why do you specify both C# and iOS? iOS uses Objective-C which is not the same as C# Commented Dec 26, 2014 at 9:24
  • mistake, it was actually for Objective-C (thus the IOS tag)...so still need that answer as Kampai decided I meant C# Commented Dec 26, 2014 at 23:49

3 Answers 3

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

pls add splitted values to array
I've added lines collection. All lines are stored there.
Can I please have example in Objective-c?
1

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

Comments

0

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

1 Comment

The stream is never closed, which will result in a resource leak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.