在文本文件中搜索数组的元素

时间:2019-01-12 13:20:24

标签: c# .net

我有两个文本文件(txt1和txt2)。每个文件包含数千个单词。我正在阅读txt1并将其拆分为一个数组,并且正在阅读txt2。我会计算一下txt2中出现的数组元素。当我打印c的值时,我必须得到c=5而不是c=2

这是相关代码部分:

string st=File.ReadAllLines("path");
int c = 0;

string[] split = st.Split(' ');

foreach (string s in split)
{
    foreach (string line in File.ReadAllLines("path"))
    {
        if (line.Contains(s))
        {
            c++; 
        }
    }
}

3 个答案:

答案 0 :(得分:1)

我不知道为什么不查看文件就得到c = 5而不是c = 2(请包括它们),但是似乎您假设第一个文本仅包含一行,因为您使用ReadAllLines,但将结果分配给一个字符串而不是一个字符串数组。可能是您得到了该结果,因为txt1的其他行中有更多单词。 如果是这种情况,则应将每一行的单词合并到“ split”变量中:

string[] st=File.ReadAllLines("path");
int c = 0;

List<string> words = new List<string>();

foeach (string s in st){
    string[] split = s.Split(' ');
    foreach(string word in split){
        words.add(word);
    }
}

foreach (string s in words)
{
    foreach (string line in File.ReadAllLines("path"))
    {
        if (line.Contains(s))
        {
            c++; 
        }
    }
}

答案 1 :(得分:1)

您循环浏览了第二个文本文件的所有行,但是您只浏览了第一个文本文件的第一行。要使用所有行,您需要像遍历第二个文件那样遍历它们:

int c = 0;
foreach (string st in File.ReadAllLines("path")) {
    string[] split = st.Split(' ');
    foreach (string s in split) {
        foreach (string line in File.ReadAllLines("path")) {
            if (line.Contains(s)) {
                c++; 
            }
        }
    }
}

尽管如此,这是一种非常低效的方式。您正在为第一个文件中的每个单词一次又一次地读取第二个文件。考虑只读取一次第二个文件,然后在循环中使用list变量:

int c = 0;
var st1 = File.ReadAllLines("path"); //the path to the first file
var st2 = File.ReadAllLines("path"); //the path to the second file

foreach (string line1 in st1) {
    foreach (string s in line1.Split(' ')) {
        foreach (string line2 in st2) {
            if (line2.Contains(s)) {
                c++; 
            }
        }
    }
}

答案 2 :(得分:1)

没有文本文件,我只能在这里假设。可能导致错误计数的最明显的原因是区分大小写。 Eg "Alex" != "alex"他们不一样。 导入两个文本文件后,它们的a都应该是小写或大写。

我确实更喜欢使用File.ReadAllText读取文件的内容并将其存储在列表中。

我的方法使用LINQ并返回匿名类型。

        var file1 = File.ReadAllText(@"path\file1.txt").Split(' ').Select(x=> x.ToLower()).ToList();
        var file2 = File.ReadAllText(@"path\file2.txt").Split(' ').Select(x => x.ToLower()).ToList();

        var result = file1.Select(x => new { Word = x, Occurance = file2.Count(c => c == x) }).ToList();