如何使用C#比较列表和文本文件 [英] How to compare a list and a text file using C#

查看:60
本文介绍了如何使用C#比较列表和文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含10行的列表。



我需要将这些行与文本文件进行比较,找到这些行的出现并增加其计数



如果我有10行。

我需要有10个计数变量才能重复计算行数。



如何执行此操作

解决方案

1.您可以创建一个包含两个属性的类来代替您的列表和计数: code> StringForSearch 和出现(计数器)然后你可以创建列表这个对象类型;



2.对于读取文件,您可以使用StreamReader逐行读取,如下例所示

< pre lang =cs> 使用(StreamReader reader = new StreamReader( file.txt))
{
string 行;
while ((line = reader.ReadLine())!= null
{
CompareLine(line,listOfStrings);
}
}



3.比较只需将上面读到的行传递给使用第1点列表搜索初始值的方法文本并增加计数器以进行匹配。


I am having a list containing 10 lines.

I need to compare these lines with a text file and find the occurrence of these line and increase their count

If I have 10 line.
I need to have 10 count variables so that it counts repeated lines.

How to do this

解决方案

1.In place of your list and counts you could create a class that contains two properties: StringForSearch and Occurrence (counter) then you could create the List of this object types;

2.For reading the file you could use StreamReader to read line by line like in the next example

using (StreamReader reader = new StreamReader("file.txt"))
{
            string line;
	    while ((line = reader.ReadLine()) != null)
	    {
		CompareLine(line, listOfStrings);
	    }
}


3.For comparing just pass the line read above to a method that use the list from the 1st point to search your initial text and to increment the counters for matching.


这篇关于如何使用C#比较列表和文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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