简体   繁体   中英

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

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,

您可以将\\r\\n放在字符串中。

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."

myString + = Environment.NewLine;

myString = myString + Environment.NewLine;
string myHTML = "some words " + Environment.NewLine + "more words");
<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM