2014-06-28 34 views
-1
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之間。

+0

是否與換行符分隔「表」的臺詞?另外,關於插入空格,如何判斷何時一個單詞結束而另一個單詞是否在沒有空格時開始?例如,「WHITEWASH」應分爲「WHITE」和「WASH」,還是保留一個單詞? –

+0

授予更新我的問題的第一個問題。我想在第1行和第2行之間添加一行。空行像第1行和第2行之間的空格/分隔符。 – user3781243

回答

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); 
+0

只需從lines.Insert(「」,1);到lines.Insert(1,「」);謝謝。 – user3781243

+0

@ user3781243:感謝您的糾正。固定。 –

相關問題