English 中文(简体)
C# 从两个杂列之间的文件获取文本
原标题:C# get text from file between two hashes

在我的 C# 程序( 在此点) 中, 我的表单有两个字段。 一个是使用列表框的单词列表; 另一个是文本框。 我成功地从文本文件向列表框装入了一个大单词列表。 我也可以这样在文本框中显示列表框中选中的项目 :

private void wordList_SelectedIndexChanged(object sender, EventArgs e)
     {
          string word = wordList.Text;
          concordanceDisplay.Text = word;
     }

我还有另外一个本地文件, 我需要在文本框中显示其中的一些内容。 在此文件中, 每个首字母( 如字典中) 前面有一个 # 。 因此, 我想在这个本地文件中使用变量单词并搜索来将条目放入文本框, 比如 :

#headword1
    entry is here...
    ...
    ...
#headword2
    entry is here...
    ...
    ...
#headword3
    entry is here...
    ...
    ...

您得到文本文件的格式。 我只需要在 # 字之前搜索正确的首字母, 然后从那里复制所有信息, 到文件的下一个散列, 然后把它放在文本框中 。

很明显,我是新来的,温柔点,多谢了

P. S. 我用溪流Reader 进入单词列表, 并在列表框中显示 :

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:PathTofile.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 + @"");
// 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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签