【发布时间】:2010-12-12 17:10:26
【问题描述】:
该程序通过将文本文件的某些部分分组到“sections”数组中来帮助用户解析文本文件。
所以问题是“是否有任何方法可以找出数组中的行号/位置?”该程序利用一个 foreach 循环来读取“sections”数组。
有人可以就代码提供建议吗?谢谢!
namespace Testing
{
class Program
{
static void Main(string[] args)
{
TextReader tr = new StreamReader(@"C:\Test\new.txt");
String SplitBy = "----------------------------------------";
// Skip 5 lines of the original text file
for(var i = 0; i < 5; i++)
{
tr.ReadLine();
}
// Read the reststring
String fullLog = tr.ReadToEnd();
String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);
//String[] lines = sections.Skip(5).ToArray();
int t = 0;
// Tried using foreach (String r in sections.skip(4)) but skips sections instead of the Text lines found within each sections
foreach (String r in sections)
{
Console.WriteLine("The times are : " + t);
// Is there a way to know or get the "r" line number?
Console.WriteLine(r);
Console.WriteLine("============================================================");
t++;
}
}
}
}
【问题讨论】:
-
投反对票。这就像您之前提出的同一个愚蠢问题的第三个化身。问题本身甚至与 API 或语言无关,它与常识有关。你要么拥有它,要么没有。更重要的是,答案几乎就在您的代码中 - 您只需将 2 和 2 放在一起。
标签: c# .net arrays string file-io