通过c#从文本文件中删除相同的行
如何使用 C# 删除文件 .txt 中的相同行但保留其中一行?
how to delete same lines in file .txt but keep one of them using c# ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何使用 C# 删除文件 .txt 中的相同行但保留其中一行?
how to delete same lines in file .txt but keep one of them using c# ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
逐行阅读。将该行添加到 HashSet,并且 Add 返回 true,将该行打印到输出。
Read line by line. Add the line to a HashSet, and Add returns true, print the line to the output.
有很多方法。一种可能是使用 Set 的某些版本。请参阅 C# Set 集合? 了解这一点。
如果所有相同的行都相继出现,您可以迭代并查看该行是否与最后一行匹配。如果是,请将其丢弃。
There are many ways. One could be to use some version of a Set. Look at C# Set collection? for this.
If all the same lines are after one another, you could just iterate through and see if the line matches the last line. If it does, discard it.
您的代码将如下所示:
与上一行一样,将其写入
输出文件。
上一行。
这假设重复的行是相邻的。如果它们不相邻,则必须先对文件进行排序。
Your code would look something like this:
as the previous line, write it to
the output file.
previous line.
This assumes that duplicate lines are adjacent. If they are not adjacent, you will have to sort the file first.