C# - 在一个文本文件中删除重复的行 [英] C# - Remove duplicate lines within a text file

查看:659
本文介绍了C# - 在一个文本文件中删除重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人证明文件是如何被检查重复的行,然后任何重复被删除或者覆盖现有文件,或创建一个新的文件,重复的行删除

Could someone demonstrate how a file is checked for duplicate lines, and then any duplicates are removed either overwriting the existing file, or create a new file with the duplicate lines removed

推荐答案

如果你使用.NET4,那么你可以使用的 File.ReadLines 和<一href="http://msdn.microsoft.com/en-us/library/dd383463.aspx"><$c$c>File.WriteAllLines:

If you're using .NET4 then you could use a combination of File.ReadLines and File.WriteAllLines:

var previousLines = new HashSet<string>();

File.WriteAllLines(destinationPath, File.ReadLines(sourcePath)
                                        .Where(line => previousLines.Add(line)));

此功能在pretty的大致相同的方式作为LINQ的分明方法,有一个重要的区别:分明终止不能保证是在相同的顺序输入序列。使用的HashSet&LT; T&GT; 明确的确提供了这种保证。

This functions in pretty much the same way as LINQ's Distinct method, with one important difference: the output of Distinct isn't guaranteed to be in the same order as the input sequence. Using a HashSet<T> explicitly does provide this guarantee.

这篇关于C# - 在一个文本文件中删除重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆