【发布时间】:2014-10-30 17:47:32
【问题描述】:
我在 While 循环中逐行读取文本文件。当我到达特定行时,我想跳过当前和接下来的 3 次迭代。
我想我可以使用计数器之类的东西来做到这一点。但我想知道是否有更优雅的方式?
using (var sr = new StreamReader(source))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line == "Section 1-1")
{
// skip the next 3 iterations (lines)
}
}
}
【问题讨论】:
-
在 if 子句中简单地执行 ReadLines 3 次,每次检查是否为空
-
@HugoRune 谢谢,这就是我所说的优雅。
-
再读 3 行,但不要使用这些行
标签: c# while-loop streamreader continue