【问题标题】:How do I delete a specific list of lines from a text document? [duplicate]如何从文本文档中删除特定的行列表? [复制]
【发布时间】:2021-03-15 00:28:28
【问题描述】:

这是我所拥有的,但没有任何关于如何删除多个特定行的内容,例如,从第 2 行到第 8 行。

var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location

【问题讨论】:

  • 您最好使用 LINQ Where 并将 IEnumerable 传递给 WriteAllLines,因为这样效率更高。

标签: c#


【解决方案1】:

您可以使用 RemoveRange,因为您已将文本文件转换为字符串列表。

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0

【讨论】:

    【解决方案2】:

    你最好的选择是这样做:

    file.RemoveRange(2, 6);
    

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 2021-03-11
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多