如何让一个文本文件在不同的 RichTextBoxes 中被一个字符串分割?

我的文本文件如下所示:


(-*-)

textA1

textA2

textA3

(-*-)

textB1

textB2

textB3

(-*-)

textC1

...

现在我尝试将文本文件按(-*-)字符串拆分(<-在文本文件中始终如此!)并将其显示在不同的富文本框中。我实际上使用以下代码...


尝试使用数据表、stringbuilder、iList ...


我的目标是让 richtextbox A 中的所有 textA,richtextbox B 中的 textB 等等......


 private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)

    {

        var seiten = new List<string>();


        if (oFDOpenDatei.ShowDialog() == DialogResult.OK)

        {                

            using (StreamReader sr = new StreamReader(oFDOpenDatei.FileName))

            {

                while (!sr.EndOfStream)

                {

                    string[] read = sr.ReadLine().Split(new string[] { "(-*-)" }, StringSplitOptions.None);


                    for (int i = 0; i < read.Length; i++)

                    {

                        seiten.Add(Convert.ToString(read));

                    }


                    //foreach (var item in read)

                    //{

                    //    seiten.Add(Convert.ToString(item));

                    //}

                }

                sr.Close();

            }


            rTBA.Text = seiten[0][0].ToString();

            rTBB.Text = seiten[1][0].ToString();

        }

    }

- 激活的“for”语句在两个 richtboxes 中显示一个“S”......无论出于何种原因:))


-foreach 说超出索引,但是当我检查 array.lenght 时,它显示 A 处为 1,B 处为 2 ... 不多


有人可以让我走上正轨吗!?


呼如林
浏览 93回答 3
3回答

繁华开满天机

首先,您似乎希望列表包含字符串数组元素,而不是字符串元素。在这种情况下,它应该是类型List<string[]>。其次,当您使用时ReadLine(),它只返回一行。如果您希望根据特定行进行拆分,则需要读取多行。这是一个使用 LINQ 的简单解决方案:var seiten = new List<string[]>();var allLines = File.ReadAllLines(oFDOpenDatei.FileName);int consumedLines = 0;while (consumedLines < allLines.Length){&nbsp; &nbsp; var group = allLines.Skip(consumedLines).TakeWhile(s => s != "(-*-)").ToArray();&nbsp; &nbsp; if (group.Any()) seiten.Add(group);&nbsp; &nbsp; consumedLines += group.Length + 1;}如果您不需要访问同一组的个别行,那么您仍然可以使用 aList<string>并将上面的代码调整为如下所示:var seiten = new List<string>();var allLines = File.ReadAllLines(oFDOpenDatei.FileName);int consumedLines = 0;while (consumedLines < allLines.Length){&nbsp; &nbsp; var group = allLines.Skip(consumedLines).TakeWhile(s => s != "(-*-)").ToArray();&nbsp; &nbsp; if (group.Any()) seiten.Add(string.Join(Environment.NewLine, group));&nbsp; &nbsp; consumedLines += group.Length + 1;}rTBA.Text = seiten[0];rTBB.Text = seiten[1];

慕桂英4014372

替代方法。所有行都从源文本文件中读取,然后分配给 a 的每个成员List<TextBoxBase>(您可以包括 RichTextBox 和 TextBox 控件),使用每次找到字符串模式时递增的选择器。如果列表中的控件数少于文本文件中的组数,则循环将仅填充列出的控件然后中断。int groupID = -1;string pattern = "(-*-)";List<TextBoxBase> controls = new List<TextBoxBase>() { rTBA, rTBB, rTBC };string[] input = File.ReadAllLines("[File Path]");for (int i = 0; i < input.Length; i++) {&nbsp; &nbsp; if (input[i].Contains(pattern)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; groupID += 1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; if (groupID == controls.Count) break;&nbsp; &nbsp; controls[groupID].AppendText(input[i] + Environment.NewLine);}

慕田峪9158850

使用正则表达式&nbsp;&nbsp;&nbsp;&nbsp;string[]&nbsp;result&nbsp;=&nbsp;Regex.Split(input,&nbsp;@"(\(-\*-\))", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RegexOptions.IgnoreCase, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TimeSpan.FromMilliseconds(500));
打开App,查看更多内容
随时随地看视频慕课网APP