You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于关键词定位文本行并提取其后第三行拆分至文本框的技术需求

Solution for Extracting and Splitting Target Line After Keyword Match

Got it, let's tweak your code to meet the exact requirements you laid out. Right now, your code only finds and displays the line with your keyword—we need to adjust it to jump to the third line after that match, then split its variables into separate text boxes. Here's how to do it:

Modified C# Code

string targetKeyword = SEARCH.Text;
string line;
bool keywordFound = false;
int linesToSkip = 3; // Track how many lines we need to move past the keyword line

using (System.IO.StreamReader file = new System.IO.StreamReader(@"D:\\TEST.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        if (!keywordFound)
        {
            // Check if we've hit our keyword line
            if (line.Contains(targetKeyword))
            {
                keywordFound = true;
                // Optional: Keep the keyword line in your rich text box if needed
                // richTextBox1.Text = line;
            }
        }
        else
        {
            linesToSkip--;
            // When we've skipped 2 lines, the next one is our target third line
            if (linesToSkip == 0)
            {
                // Split the target line into variables (adjust the delimiter to match your file)
                // Replace ',' with your actual separator (e.g., ' ', ';', '\t')
                string[] variables = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                
                // Assign variables to separate text boxes (update names to match your UI)
                if (variables.Length >= 1) textBox1.Text = variables[0].Trim();
                if (variables.Length >= 2) textBox2.Text = variables[1].Trim();
                if (variables.Length >= 3) textBox3.Text = variables[2].Trim();
                // Add more entries here if you have additional variables
                
                // Optional: Show the full target line in your rich text box
                richTextBox1.Text = line;
                
                break; // Exit the loop once we've processed the target line
            }
        }
    }

    // Handle edge cases where the keyword wasn't found or there aren't enough lines after it
    if (!keywordFound || linesToSkip > 0)
    {
        MessageBox.Show("Keyword not found, or there aren't enough lines after the match.");
    }
}

Key Adjustments Explained

  • Keyword tracking: We added a keywordFound flag to switch our logic once we locate the line with your keyword.
  • Line counting: The linesToSkip counter starts at 3—each time we read a line after the keyword, we decrement it. When it hits 0, we know we're on the third line after the match.
  • Variable splitting: The Split() method breaks the target line into parts. Make sure to update the delimiter (like ',' in the code) to match how variables are separated in your TEST.txt file (could be spaces, semicolons, tabs, etc.).
  • Error safety: We added checks to avoid index-out-of-range errors if the line has fewer variables than expected, plus a message to notify you if the keyword isn't found or there aren't enough lines after it.

Quick Notes

  • Update the text box names (textBox1, textBox2, etc.) to match the ones in your project's UI.
  • If you need to handle multiple keyword matches (not just the first one), remove the break; statement and reset the keywordFound and linesToSkip variables after processing each match.

内容的提问来源于stack exchange,提问作者Reda HellJoka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:56:56