gpt4 book ai didi

c# - 从文本文件中删除特定行?

转载 作者:IT王子 更新时间:2023-10-29 04:22:53 25 4
gpt4 key购买 nike

我需要从文本文件中删除确切的一行,但我一辈子都弄不明白该怎么做。

如有任何建议或示例,我们将不胜感激?

相关问题

Efficient way to delete a line from a text file (C#)

最佳答案

如果要删除的行是基于该行的内容:

string line = null;
string line_to_delete = "the line i want to delete";

using (StreamReader reader = new StreamReader("C:\\input")) {
using (StreamWriter writer = new StreamWriter("C:\\output")) {
while ((line = reader.ReadLine()) != null) {
if (String.Compare(line, line_to_delete) == 0)
continue;

writer.WriteLine(line);
}
}
}

或者如果是基于行号:

string line = null;
int line_number = 0;
int line_to_delete = 12;

using (StreamReader reader = new StreamReader("C:\\input")) {
using (StreamWriter writer = new StreamWriter("C:\\output")) {
while ((line = reader.ReadLine()) != null) {
line_number++;

if (line_number == line_to_delete)
continue;

writer.WriteLine(line);
}
}
}

关于c# - 从文本文件中删除特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1245243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com