How can I create a carriage return in my C# string

2020-03-08 11:45发布

I have C# code that creates HTML. One particular piece of code creates a long string which is no problem for the browser. However when I look at the code with view > source it's difficult to see what's happening.

Is there some way that I can insert a carriage return or new line into my string so that it will make the string continue on the next line.

Thanks,

标签: c#
5条回答
Ridiculous、
2楼-- · 2020-03-08 12:16

You can put \r\n in your string.

查看更多
Deceive 欺骗
3楼-- · 2020-03-08 12:17

Along with Environment.NewLine and the literal \r\n or just \n you may also use a verbatim string in C#. These begin with @ and can have embedded newlines. The only thing to keep in mind is that " needs to be escaped as "". An example:

string s = @"This is a string
that contains embedded new lines,
that will appear when this string is used."
查看更多
乱世女痞
4楼-- · 2020-03-08 12:17
string myHTML = "some words " + Environment.NewLine + "more words");
查看更多
家丑人穷心不美
5楼-- · 2020-03-08 12:19
<br /> works for me

So...

String body = String.Format(@"New user: 
 <br /> Name: {0}
 <br /> Email: {1}
 <br /> Phone: {2}", Name, Email, Phone);

Produces...

New user:
Name: Name
Email: Email
Phone: Phone

查看更多
Deceive 欺骗
6楼-- · 2020-03-08 12:38

myString += Environment.NewLine;

myString = myString + Environment.NewLine;
查看更多
登录 后发表回答