0

我有一个单词列表。我希望程序从文本文件中扫描多个单词。

这是我已经拥有的:

int counter = 0;
        string line;
        StringBuilder sb = new StringBuilder();

        string[] words = { "var", "bob", "for", "example"};

        try
        {
            using (StreamReader file = new StreamReader("test.txt"))
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(Convert.ToChar(words)))
                    {
                        sb.AppendLine(line.ToString());
                    }
                }
            }

            listResults.Text += sb.ToString();
        }
        catch (Exception ex)
        {
            listResults.ForeColor = Color.Red;
            listResults.Text = "---ERROR---";
        }

所以我想扫描文件中的一个单词,如果它不存在,扫描下一个单词...

4

3 回答 3

2

String.Contains()只接受一个参数:一个字符串。你的电话做什么Contains(Convert.ToChar(words)),可能不是你所期望的。

正如Using C# to check if string contains a string in string array 中所述,您可能想要执行以下操作:

using (StreamReader file = new StreamReader("test.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        foreach (string word in words)
        {
            if (line.Contains(word))
            {
                sb.AppendLine(line);
            }
        }
    }
}

或者,如果您想遵循您的确切问题陈述(“扫描文件中的一个单词,如果它不存在,则扫描下一个单词”),您可能需要查看Return StreamReader to Starting

using (StreamReader file = new StreamReader("test.txt"))
{
    foreach (string word in words)
    {
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains(word))
            {
                sb.AppendLine(line);
            }
        }

        if (sb.Length == 0)
        {
            // Rewind file to prepare for next word
            file.Position = 0;
            file.DiscardBufferedData();   
        }
        else
        {
            return sb.ToString();
        }
    }
}

但这会认为“bob”是“bobcat”的一部分。如果您不同意,请参阅String compare C# - whole word match并替换:

line.Contains(word)

string wordWithBoundaries = "\\b" + word + "\\b";
Regex.IsMatch(line, wordWithBoundaries);
于 2014-01-29T19:01:32.683 回答
0
StringBuilder sb = new StringBuilder();             
string[] words = { "var", "bob", "for", "example" };
string[] file_lines = File.ReadAllLines("filepath");
for (int i = 0; i < file_lines.Length; i++)         
{                                                   
    string[] split_words = file_lines[i].Split(' ');
    foreach (string str in split_words)             
    {                                               
        foreach (string word in words)              
        {                                           
            if (str == word)                        
            {                                       
                sb.AppendLine(file_lines[i]);       
            }                                       
        }                                           
    }                                               
}                                                   
于 2014-01-29T19:07:40.127 回答
0

这是一种享受:

var query =
    from line in System.IO.File.ReadLines("test.txt")
    where words.Any(word => line.Contains(word))
    select line;

要将它们作为单个字符串取出,只需执行以下操作:

var results = String.Join(Environment.NewLine, query);

再简单不过了。


如果您只想匹配整个单词,它只会变得更复杂一些。你可以这样做:

Regex[] regexs =
    words
        .Select(word => new Regex(String.Format(@"\b{0}\b", Regex.Escape(word))))
        .ToArray();

var query =
    from line in System.IO.File.ReadLines(fileName)
    where regexs.Any(regex => regex.IsMatch(line))
    select line;
于 2015-02-06T11:53:36.847 回答