所以,我生成了一个字符串,它包含一个标题,然后是一个描述。
示例:
Item1 Description1 Item2 Description2等等。
我希望能很好地格式化这些内容,例如:
Item1
Description1
Item 2
Description2对于HTML格式,回车符正在被<br />所取代。
我有以下代码,用<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 />替换每个奇数实例,才能正确地使用这种格式。
有人能帮帮我吗?
我希望这是有意义的。谢谢
发布于 2014-07-08 04:42:50
您在这里有一些选择,例如,您可以使用正则表达式来解决一些问题,但我只会走简单的路线,并使用循环。我是一个使用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 Description1...“中包含这些回车。所以我根据你剩下的问题添加了它们。如果你的意思不同,请告诉我。
https://stackoverflow.com/questions/24619286
复制相似问题