c# - 在 C# 中,在附加到文本文件的行上方和下方写入 n 行

标签 c#

在我正在开发的应用程序中,我创建了关键字搜索器。用户上传一个文本文件(因为这是为了工作,所以它将是一个包含数千行消息的 txt 文件),并且可以输入多个单词进行搜索,应用程序会提取具有相应输入的任何行。唯一的问题是,他们希望能够提取正在复制的行上方和下方的 n 行,以便他们可以看到正在提取的消息的上下文。有没有办法说复制这一行以及这一行上方和下方的n行?下面是我搜索该单词并编写它的代码。

 private void button12_Click(object sender, EventArgs e)
 {
     string[] sArray = System.IO.File.ReadAllLines(textBox7.Text);
     StringBuilder sb = new StringBuilder();

     foreach (string line in sArray)
     {
          if (Regex.IsMatch(line, (textBox9.Text), RegexOptions.IgnoreCase) && !string.IsNullOrWhiteSpace(textBox9.Text))
          {
              sb.AppendLine(line);
          }

          if (Regex.IsMatch(line, (textBox10.Text), RegexOptions.IgnoreCase) && !string.IsNullOrWhiteSpace(textBox10.Text))
          {
              sb.AppendLine(line);
          }
        }

        using (StreamWriter sw = new StreamWriter(textBox8.Text))
        {
            sw.Write(sb);
        }
    }
}

最佳答案

我提供了一个小例子。您可以像这样使用它:

List<string> lines = new List<string>() {"This", "is", "some", "test", "data"};
List<string> result = GetMatchingLines(lines, "test", 2, 2);

该方法如下所示:

/// <summary>
/// Gets all lines containing the "match" including "before" lines before and "after" lines after.
/// </summary>
/// <param name="lines">The original lines.</param>
/// <param name="match">The match that shall be found.</param>
/// <param name="before">The number of lines before the occurence.</param>
/// <param name="after">The number of lines after the occurence.</param>
/// <returns>All lines containing the "match" including "before" lines before and "after" lines after.</returns>
private List<string> GetMatchingLines(List<string> lines, string match, int before = 0, int after = 0)
{
    List<string> result = new List<string>();

    for (int i = 0; i < lines.Count; i++)
    {
        if (string.IsNullOrEmpty(lines[i]))
        {
            continue;
        }

        if (Regex.IsMatch(lines[i], match, RegexOptions.IgnoreCase))
        {
            for (int j = i - before; j < i + after; j++)
            {
                if (j >= 0 && j < lines.Count)
                {
                    result.Add(lines[j]);
                }
            }
        }
    }

    return result;
}

考虑到您的代码,该方法将以如下方式调用:

string[] lines = File.ReadAllLines(textBox7.Text);
List<string> result = new List<string>();

if (!string.IsNullOrEmpty(textBox9.Text))
{
    result.AddRange(GetMatchingLines(lines.ToList(), textBox9.Text, 2, 2));
}

if (!string.IsNullOrEmpty(textBox10.Text))
{
    result.AddRange(GetMatchingLines(lines.ToList(), textBox10.Text, 2, 2));
}

File.WriteAllLines(textBox8.Text, result);

关于c# - 在 C# 中,在附加到文本文件的行上方和下方写入 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22664909/

相关文章:

c# - 如何使用重写中间件指定不区分大小写的规则?

c# - 如何异步读取 XML 文件?

c# - WP8 AudioPlaybackAgent 错误?

c# - ComboBox 元素的样式

c# - 创建表单(没有表单设计器)

c# - 如何使用 DateTime.Parse() 创建 DateTime 对象

c# - MVVM - 按钮命令绑定(bind)不起作用

c# - 使用 IronPython 通过 C# .NET 导入 Python 模块

c# - 将表达式解析为参数

c# - 将值从一个对象复制到另一个对象(不同类型)