簡體   English   中英

試圖忽略空行來解析文本文件中最后一次出現的字符串

[英]Trying to ignore blank lines to parse the last occurrence of a string in a text file

所以我有這個文本文件的結構如下:

Date: 12/23/23
.
.
.
Date: 12/23/66
.
.
Date: 12/25/67

為此,我試圖獲取日期的最后一次發生,即:

Date: 12/25/67

有了這個,我已經知道如何通過這段代碼:

string date = "";
foreach(string line in File.ReadAllLines("mytext.txt"))
{
    date = GetDate_value(line, date);    
}

public static string GetDate_value(string line, string date)
{ 
    if (line.Contains("Date:"))
    {
        date = line;   
    }

    string[] separatingStrings = { "Date: " };
    string[] str = date.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
    Console.WriteLine("[{0}]", string.Join(", ", str));
    return str.Last();
}

有了這個,當我返回日期時,我得到了最后一次出現。 但是,我試圖將其進一步解析到僅包含 12/25/67 的位置。

有了這個,我知道上面的邏輯如何。 但是,當我嘗試解析文件時,我得到一個錯誤,說元素中的數組不存在。 進一步檢查顯示與我的Console.WriteLine數組顯示:

[]
[]
[]
[]
[  , 12/25/67]
[  , 12/25/67]
[  , 12/25/67]
[  , 12/25/67]
.
.(repeats for how many lines in text file)

我想知道為什么我在開始部分沒有得到任何元素,而一切都應該是:

[  , 12/25/67]
[  , 12/25/67]
[  , 12/25/67]
[  , 12/25/67]

我嘗試使用它來替換foreach行:

foreach (string line in File.ReadAllLines(file).Where(line => !string.IsNullOrWhiteSpace(line)))

但是它仍然沒有忽略最后一行。 我將如何使它在開始時忽略 null 元素?

編輯

因此,在獲取日期 function 中使用這個 if 語句:

string[] separatingStrings = { "Date: " };
string[] str = date.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
if(str.Length == 0)
{

    Console.WriteLine("null");

}

我發現這些values([])的長度為 0,那么我怎么能跳過長度 0 arrays 來忽略它?

也許嘗試這樣做:

string last =
    File
        .ReadLines("mytext.txt")
        .Aggregate(
            (string)null,
            (a, x) =>
                x.StartsWith("Date: ")
                    ? x.Substring("Date: ".Length) 
                    : a)

如果lastnull那么您的文本文件不包含以"Date: "開頭的行,否則它將包含文件中的最后一個日期(盡管仍然是一個字符串)。

我還選擇了File.ReadLines ,因為它會懶惰地讀取文件,因此文件大小應該不是問題。

您的代碼片段中的問題是GetDate_value function,無論傳遞的行是否包含Date: :,您都在其中執行相同的代碼。 您需要將所有內容移至if塊以返回日期,否則 function 應返回nullstring.Empty

void TheCaller()
{
    var f = "myText.txt";
    var dates = new List<string>();

    foreach (string line in File.ReadLines(f)
        .Where(l => l.Length > 6)) //At least...
    {
        var date = GetDate_value(line);

        if (date != null)
            dates.Add(date);
    }

    Console.WriteLine("[{0}]", string.Join(", ", dates));
}

public static string GetDate_value(string line)
{
    string output = null;

    if (line.StartsWith("Date:"))
    {
        output = line.Split(new[] { " " }, 
            StringSplitOptions.RemoveEmptyEntries).LastOrDefault();

    }
    return output;
}

您可以將上述內容替換為:

void TheCaller()
{
    var f = "myText.txt";
    var dates = File.ReadLines(f)
        .Where(l => l.StartsWith("Date: "))
        .Select(l => l.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
        .LastOrDefault()).ToList();

    Console.WriteLine("[{0}]", string.Join(", ", dates));
}

給定示例的 output 是:

[12/23/23, 12/23/66, 12/25/67]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM