2014-07-07 36 views
0

所以,我有一個生成的字符串,其中包含標題,然後是描述。查找並替換所有其他實例

例子:

Item1 Description1 Item2 Description2 

等等

我期待良好的格式化這些,如:

Item1 
Description1 

Item 2 
Description2 

回車被替換爲<br /> HTML格式。

我有以下代碼,用<br /><br />標記替換每個回車符。

'//Replace return key with <br />\\ + Debugger 
Dim errString As String = EmailBody.ToString 

FrmDebug.Label1.Text = "Original String: " 
FrmDebug.TextBox1.Text = errString 

' Correct the spelling of "document". 
Dim correctString As String = errString.Replace(ChrW(Keys.Return), "<br />") 

FrmDebug.Label2.Text = "Corrected String: " 
FrmDebug.TextBox2.Text = correctString 
'\\Replace return key with <br />// 

不過,我想知道如何使其與1 <br />並在此與2 <br /><br />每個奇數實例來替換這個曾經甚至實例,以便正確有這種格式。

有人可以幫我嗎?

我希望這是有道理的。謝謝

回答

0

你在這裏有幾個選擇,例如你可以用正則表達式工作,但我只是去簡單的路線和使用循環。我是一個C#的傢伙,但我相信你能明白這不夠好,做的相當的VB:

string arg = "Item1\rDescription1\rItem2\rDescription2"; 
StringBuilder ret = new StringBuilder(); 
bool isSecond = false; 
for(int chIndex = 0; chIndex < arg.Length; chIndex++) 
{ 
    char ch = arg[chIndex]; 

    if(ch == '\r') 
    { 
     ret.Append("<br />"); 

     if (isSecond) 
      ret.Append("<br />"); 

     isSecond = !isSecond; 
    } 
    else 
    { 
     ret.Append(ch); 
    } 
} 

我注意到你的原來的例子並不包括在「Item1的內容描述那些回車.. 。「所以我根據你的問題的其餘部分添加它們。讓我知道你的意思是不同的。