2

I have the following code:

class Program
{
    static void Main(string[] args)
    {
        string linie;
        foreach (string elem in Directory.GetFiles(@"C:\Users\A\Desktop\FIles", "*.txt"))
        {
            Console.WriteLine(elem);
            StreamReader reader = new StreamReader(elem);
            {
                while (!reader.EndOfStream)
                {
                    linie=reader.ReadLine();
                    Console.WriteLine(linie);                         
                }
            }
            reader.Close();

        }
        Console.ReadKey();
        Console.WriteLine(DateTime.ParseExact("5/10/2005", "m/d/yyyy", null).Day);
    }
}

What i need is to select only the Date from a file.
For example if i have the string "the date is 20/2/2012" in a .txt file, i need to substract only 20/2/2012 and to compare it with the current date.

4

1 回答 1

2

如果您想要一个简单的惰性解决方案,您可以随时添加 a:Split。(你可以在空格上分割,但我必须计算索引,我不想这样做)。

string   dateFromFile = "The date is : 20/2/2012";
string[] dateString =  dateFromFile.Split(':');

string myDate = dateString[1];

好吧,我看了我的答案,决定我太懒了......

string dateFromFile = "The date is 20/2/2012";
string[] dateString = dateFromFile.Split(' ');

string myDate = dateString[3];

每次看到经过分隔的字符时,它都会拆分字符串并返回一个String[].

在第二个示例中(我在空白处拆分,数组看起来像这样)

dateString[0] = "The"
dateString[1] = "date"
dateString[2] = "is"
dateString[3] = "20/2/2012"
于 2013-04-26T23:13:09.833 回答