我如何在单个字符串变量的第一行和第二行之间添加空格/空行

本文关键字:二行 空行 空格 添加 之间 一行 单个 字符串 变量 | 更新日期: 2023-09-27 18:10:33

string tables = this.webBrowser1.Document.GetElementById("tblProducts").InnerText;
            using (StringReader reader = new StringReader(tables))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    // Do something with the line
                }
            }

这就是我现在所做的:

string tables = this.webBrowser1.Document.GetElementById("tblProducts").InnerText;
            StreamWriter w = new StreamWriter(@"c:'temp'Table1.txt");
            w.WriteLine(tables);
            w.Close();
            string[] lines = File.ReadAllLines(@"c:'temp'Table1.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                lines[i] = lines[i].Insert(0, "here ADDED TEXT");
            }

而不是插入,我想在第1行和第2行之间添加一个新行。所以如果一行现在包含31行,那么最后它应该包含32行,在第一行和第二行之间的新行。

我如何在单个字符串变量的第一行和第二行之间添加空格/空行

认为你是在说,给定这样的文本:

This is line 1
Second line here
And a third line

你想要的是:

This is line 1
<empty line here>
Second line here
And a third line

如果是这样,这应该很容易做到:

string tables = this.webBrowser1.Document.GetElementById("tblProducts").InnerText;
var lines = tables.Split(new[]{Environment.NewLine}, StringSplitOptions.None).ToList();
lines.Insert(1, "");
string newText = string.Join(Environment.NewLine, lines);