[英]Reading a text file separated by dotted lines in C#
我的應用程序日志文件中的文本由 ---- 虛線分隔。 我正在將該文本作為一個字符串閱讀。
字符串是這樣的:
8/23 17:40:54:761 6948 - AUDIT - Logging: Set ABC
-----------------------------------------
08/23 17:40:54:772 6948 - TRANSACTION - Logging: FullFileName:/XYZ/
Message:Some Message
Type : Information
---------------------------------------------
08/23 17:40:54:844 6948 - INFO - Logging: End of Control_Loaded
如何將此單個字符串轉換為字符串數組或列表,以便每個字符串在虛線之間都有一個段落。 例如:AUDIT 段落為字符串[0],TRANSACTION 段落為字符串[1] 等等......直到最后沒有虛線的段落。
這是我嘗試過的:
string textFromFile = _streamReader.ReadToEnd();
string[] paragraphs = textFromFile.Split(
new char[] { '-','-','-','-','-','-'}, StringSplitOptions.RemoveEmptyEntries);
你有 go:
IEnumerable<string> ReadParagraphs(IEnumerable<string> source)
{
var output = new List<string>();
foreach (var line in source)
{
if (line == "---------------------------------------------")
{
if (output.Count > 0)
{
yield return String.Join(Environment.NewLine, output);
output.Clear();
}
}
else
{
output.Add(line);
}
}
if (output.Count > 0)
{
yield return String.Join(Environment.NewLine, output);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.