Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中,在追加到文本文件的行的上方和下方写n行#_C# - Fatal编程技术网

C# 在C语言中,在追加到文本文件的行的上方和下方写n行#

C# 在C语言中,在追加到文本文件的行的上方和下方写n行#,c#,C#,在我正在开发的应用程序中,我创建了关键字搜索器。用户上传一个文本文件(因为这是为了工作,它将是一个包含数千行消息的txt文件),并且可以输入多个单词进行搜索,应用程序会用相应的输入拉出任何一行。唯一的问题是,他们还希望能够在被复制行的上方和下方拉出n行,这样他们就可以看到被拉出消息的上下文。有没有办法说复制这一行以及这一行上面和下面n行的数目?下面是我搜索并编写单词的代码 private void button12_Click(object sender, EventArgs e) {

在我正在开发的应用程序中,我创建了关键字搜索器。用户上传一个文本文件(因为这是为了工作,它将是一个包含数千行消息的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);
        }
    }
}

由于要查找行之间存在整数差的不同行,因此可能需要将
foreach
编辑为
for

这样你就可以说

var thisLine = sArray[i];
var oneLineBefore = sArray[i-1];
var oneLineAfter = sArray[i+1];
sb.Append(oneLineBefore);
sb.Append(thisLine);
sb.Append(oneLineAfter);

显然,在访问之前,请确保进行检查以确保前后两行都存在。

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

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);
List lines=newlist(){“This”、“is”、“some”、“test”、“data”};
列表结果=获取匹配行(行,“测试”,2,2);
方法如下所示:

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);
//
///获取包含“匹配”的所有行,包括前面的“before”行和后面的“after”行。
/// 
///原来的台词。
///应该找到的匹配项。
///发生前的行数。
///发生后的行数。
///包含“匹配”的所有行,包括前面的“before”行和后面的“after”行。
私有列表GetMatchingLines(列表行、字符串匹配、int-before=0、int-after=0)
{
列表结果=新列表();
对于(int i=0;i=0&&j
考虑到您的代码,该方法将以如下方式调用:

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);
string[]lines=File.ReadAllLines(textBox7.Text);
列表结果=新列表();
如果(!string.IsNullOrEmpty(textBox9.Text))
{
AddRange(GetMatchingLines(lines.ToList(),textBox9.Text,2,2));
}
如果(!string.IsNullOrEmpty(textBox10.Text))
{
AddRange(GetMatchingLines(lines.ToList(),textBox10.Text,2,2));
}
File.writeAllines(textBox8.Text,结果);

所以你想构建grep,对吗?我将使用
for
而不是
foreach
。通过这种方式,您可以“知道”匹配的行号,并做任何您想做的事情;-)嗯,我不知道什么是grep,想解释一下吗?我已经发布了一个小例子,说明如何实现grep。你必须每个单词调用这个方法一次。太棒了!我会看一看,然后让你知道它是怎么回事,谢谢。你发布的两个方法都是一样的吗?只是我的代码中有一个?为什么它们看起来如此不同?最后一个展示了如何使用我在示例代码上下文中编写的方法;-)所以你可以在按钮点击事件中写下最后的代码。顺便说一句,当我得到15点声誉积分时,我肯定会回来,因为我现在有13点,所以再多两个:)老兄,你真是个救命恩人。明天早上我将向我所有的老板和主管提交这份申请。我原以为我不能在开会前完成申请表的这一部分,但多亏了你,我才完成了!非常感谢,非常感谢!向上投票!