C#在两个散列之间从文件中获取文本

C#在两个散列之间从文件中获取文本,c#,streamreader,C#,Streamreader,在我的C#程序中(此时),我的表单中有两个字段。一种是使用列表框的单词列表;另一个是文本框。我已经能够成功地将一个大单词列表从文本文件加载到列表框中。我还可以通过以下方式将列表框中的选定项目显示到文本框中: private void wordList_SelectedIndexChanged(object sender, EventArgs e) { string word = wordList.Text; concordanceDisplay.

在我的C#程序中(此时),我的表单中有两个字段。一种是使用列表框的单词列表;另一个是文本框。我已经能够成功地将一个大单词列表从文本文件加载到列表框中。我还可以通过以下方式将列表框中的选定项目显示到文本框中:

private void wordList_SelectedIndexChanged(object sender, EventArgs e)
     {
          string word = wordList.Text;
          concordanceDisplay.Text = word;
     }
我有另一个本地文件,我需要在文本框中显示它的一些内容。在这个文件中,每个标题词(如在字典中)前面都有一个#。因此,我想使用变量“word”并在本地文件中搜索,将条目放入文本框,如下所示:

您可以获得文本文件的格式。我只需要在单词前加#搜索正确的标题词,然后将所有信息从那里复制到文件中的下一个哈希,并将其放入文本框中

显然,我是个新手,所以要温柔。多谢

另外,我使用StreamReader获取单词列表并将其显示在列表框中,如下所示:

StreamReader sr = new StreamReader("C:\\...\\list-final.txt");
       string line;
       while ((line = sr.ReadLine()) != null)
       {
           MyList.Add(line);
       }
       wordList.DataSource = MyList;

与其在每行上调用子字符串,为什么不只查找“#”+单词:)还可以使用line.Equals(“#”+单词,StringComparison.CurrentCultureIgnorCase)。最后,我将为字典文件编制索引,以便更快地查找。我只在#行上调用子字符串。无论哪种方法都可以,使用子字符串只是我第一次想到的。如果询问者想要进行CurrentCultureInogoreCase比较,这是一个很好的方法。谢谢你的帮助。我在这里感到困惑:
inCorrectSection=line.Substring(1)==sectionName。您似乎正在将子字符串值应用于变量
inCorrectSection
,该变量为布尔值。我一定是遗漏了什么。这也可能是写的
inCorrectSection=(line.Substring(1)=sectionName)。也就是说,我将第一个字符后的
节名
进行比较。其结果是一个布尔值,然后我将其存储在
inCorrectSection
.inCorrectSection=(line.Substring(1)=sectionName);这可能更清楚。括号是行和要查找的单词之间的比较。它是一个布尔值,然后会影响isCorrectSection。与其在每一行上调用子字符串,为什么不只查找“#”+单词:)还可以使用line.Equals(“#”+单词,StringComparison.CurrentCultureIgnoreCase)。最后,我将为字典文件编制索引,以便更快地查找。我只在#行上调用子字符串。无论哪种方法都可以,使用子字符串只是我第一次想到的。如果询问者想要进行CurrentCultureInogoreCase比较,这是一个很好的方法。谢谢你的帮助。我在这里感到困惑:
inCorrectSection=line.Substring(1)==sectionName。您似乎正在将子字符串值应用于变量
inCorrectSection
,该变量为布尔值。我一定是遗漏了什么。这也可能是写的
inCorrectSection=(line.Substring(1)=sectionName)。也就是说,我将第一个字符后的
节名
进行比较。其结果是一个布尔值,然后我将其存储在
inCorrectSection
.inCorrectSection=(line.Substring(1)=sectionName);这可能更清楚。括号是行和要查找的单词之间的比较。这是一个布尔值,然后会影响isCorrectSection。您的大单词列表有多大?所有的单词都能一次放入内存吗?你的单词列表有多大?这一切是否一下子都能记在记忆中?
StreamReader sr = new StreamReader("C:\\...\\list-final.txt");
       string line;
       while ((line = sr.ReadLine()) != null)
       {
           MyList.Add(line);
       }
       wordList.DataSource = MyList;
string getSection(string sectionName)
{
    StreamReader sr = new StreamReader(@"C:\Path\To\file.txt");
    string line;
    var MyList = new List<string>();
    bool inCorrectSection = false;
    while ((line = sr.ReadLine()) != null)
    {
        if (line.StartsWith("#"))
        {
            if (inCorrectSection)
                break;
            else
                inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");
        }
        else if (inCorrectSection)
            MyList.Add(line);
    }
    return string.Join(Environment.NewLine, MyList);
}

// in another method
textBox.Text = getSection("headword1");
// if the separator after the section name is always " -", this is the best way I've thought of, since it will work regardless of what's in the sectionName
inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"($| -)");
// as long as the section name can't contain # or spaces, this will work
inCorrectSection = line.Split('#', ' ')[1] == sectionName;
// as long as only alphanumeric characters can ever make up the section name, this is good
inCorrectSection = Regex.IsMatch(line, @"^#" + sectionName + @"\b");
// the problem with this is that if you are searching for "head", it will find "headOther" and think it's a match
inCorrectSection = line.StartsWith("#" + sectionName);
var sectionLines = File.ReadAllLines(fileName) // shortcut to read all lines from file
    .SkipWhile(l => l != "#headword2") // skip everything before the heading you want
    .Skip(1) // skip the heading itself
    .TakeWhile(l => !l.StartsWith("#")) // grab stuff until the next heading or the end
    .ToList(); // optional convert to list