读取文本文件并比较上一行是否等于当前行,然后忽略 [英] Read text file and compare if the previous line is equal to the current and ignore

查看:103
本文介绍了读取文本文件并比较上一行是否等于当前行,然后忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试比较文本文件中的行,但是我看到的示例有些复杂,有人可以给我一个简单的示例.

例子:

"123","234"
"123","234"将此行与顶部进行比较
"134","664"

最后只显示以下内容:

"123","234"
"134","664"

谢谢

代码:

Hi ,

I''m trying to compare lines in a text file , but the examples I''ve seen are a bit complicated someone could give me a simple example of how to do it .

example :

"123" , " 234 "
"123" , " 234 " Compare this line to the top
" 134 " , " 664 "

and in the end only show this:

"123" , " 234 "
" 134 " , " 664 "

Thanks

Code:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections;

namespace ReadTxt
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader read = new StreamReader(@"C:\File.txt");
            String line = read.ReadToEnd();
            
            //Do stuff

                Console.WriteLine(line);
                Console.ReadLine();
            
            }           
        }
 }

推荐答案

好.假设您需要通过char相等来比较使用char的行,它可以像这样简单:
Well. Supposing that you need to compare lines using char by char equality, it can be as simple as this:
var result = File.ReadLines(@"D:\TEMP\x.txt").Distinct();


不过,如果您需要性能,则需要更深入地进行优化.我不得不承认,这不是为仅比较两个连续的行而优化的.它正在进行整体搜索.
要仅考虑上一行,可以改用以下一行:


Still, if you need performance, you need to dig deeper to optimize. I have to admit, this is not optimized for comparing only the two consecutive rows. It is doing overall search.
To take only previous line into account, you can use this one instead:

string prev = null; 
foreach(var curr in File.ReadLines(@"D:\TEMP\x.txt"))
{
	if(curr != prev) Console.WriteLine(curr);
	prev = curr;
}


它不需要想法".请参阅我对答案的评论.
正在读取文件?请参阅 http://msdn.microsoft. com/en-us/library/system.io.file.readalllines%28v = vs.110%29.aspx [ http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v= vs.110%29.aspx [ ^ ].
行比较? ''==''.
弄清楚要与什么进行比较?使用此方法: http://en.wikipedia.org/wiki/Brain [ http://msdn.microsoft.com/en-us/library /yh598w02.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/aa664736%28v = vs.71%29.aspx [ ^ ].

更重要的是,在之前使用我的最后一个链接.为什么要读到最后?读到最后的文本不是一个文本行.您需要一一阅读.

—SA
It does not require "ideas". Please see my comment to the answer.
Reading file? Please see http://msdn.microsoft.com/en-us/library/system.io.file.readalllines%28v=vs.110%29.aspx[^] or http://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^].
Line comparison? ''==''.
Figuring out what to compare with what? Use this: http://en.wikipedia.org/wiki/Brain[^].



Now I can see your updated question. First, better use using statement: http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^],
http://msdn.microsoft.com/en-us/library/aa664736%28v=vs.71%29.aspx[^].

More importantly, use my last link before . Why reading to the end? The text read to the end is not one text line. You need to read line one by one.

—SA


这篇关于读取文本文件并比较上一行是否等于当前行,然后忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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