开发者

delete same lines from text file by c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago.

how to delete same li开发者_StackOverflow社区nes in file .txt but keep one of them using c# ?


Read line by line. Add the line to a HashSet, and Add returns true, print the line to the output.


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:

  1. For each line in the input file.
  2. If the current line is not the same as the previous line, write it to the output file.
  3. Set the current line equal to the previous line.

This assumes that duplicate lines are adjacent. If they are not adjacent, you will have to sort the file first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜