简体   繁体   English

如何在C#字符串中创建回车符

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

I have C# code that creates HTML. 我有C#代码创建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. 但是,当我使用view> source查看代码时,很难看到发生了什么。

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#. Environment.NewLine和文字\\r\\n或只是\\n你也可以在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 电话:电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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