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

时间:2014-06-28 02:17:13

标签: c# .net winforms

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");
            }

但改为插入iwant以某种方式在第1行和第2行之间添加新行。因此,如果行现在包含31行,那么最后它应包含32行,第1行和第2行之间的新行。在结尾添加一个新行,但在1到2之间。

1 个答案:

答案 0 :(得分:2)

认为你说的是,给定的文字如下:

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);
相关问题