在字符串中插入换行符 [英] Insert newline in string
问题描述
大家好,
我正在尝试构造要发送给用户的消息.
在添加签名之前,我要插入换行符.
我正在使用以下代码.
hi all,
i m trying to contruct messages to be sent to users.
befroe adding the signature i want to insert a newline.
i m using the below code.
Message = Message + Environment.NewLine;
Message = Message.Replace("#Signature#",
obju.Signature);
但邮件发送时没有换行符.
谁能帮忙吗?
but the messages are sent without a newline.
can anyone please help?
thanks.
推荐答案
由于这是ASP.NET,请尝试将Environment.NewLine
替换为< br/>"
Since this is ASP.NET, try replacing Environment.NewLine
with "<br />"
您插入不正确.唯一合理的方法是:
You insert incorrectly. The only reasonable way is:
string format = "My first line;{0}My second line";
//...
string separatedForTextBox = string.Format(format, System.Environment.NewLine);
string separatedForHtml = string.Format(format, "<br />");
请记住:字符串是不可变的,因此"+"效率不高. (我必须解释为什么吗?)
Please keep in mind: string is immutable, so "+" is not efficient. (Do I have to explain why?)
问题是您在消息末尾添加了新行,因此
如果您的留言是
The problem is you are adding new line at the end of the message so
if your message is
Message = "abcd #signature#";
第一行代码Message = Message + Environment.NewLine;
将导致
the first code line Message = Message + Environment.NewLine;
will result in
Message = "abcd #signature# + NewLine character"
第二行
and the second line
Message = Message.Replace("#Signature#",
obju.Signature);
将在换行符之前替换签名,因此最终结果将是:
消息="abcd TheSignature +换行符"
解决方案:
像这样修改上面的代码:
will replace the signature before new line character hence end result will be:
Message = "abcd TheSignature + NewLine character"
Solution:
Modify above code like this:
//Message = Message + Environment.NewLine;
Message = Message.Replace("#Signature#",
Environment.NewLine + obju.Signature);
希望对您有所帮助.
谢谢,
hemant
Hope it will help.
Thanks,
hemant
这篇关于在字符串中插入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!