如何在字符串文字中插入换行符?
在 .NET 中,我可以提供 \r
或 \n
字符串文字,但有一种方法可以插入 诸如“新行”特殊字符之类的东西,例如 Environment.NewLine
静态属性?
In .NET I can provide both \r
or \n
string literals, but there is a way to insert
something like "new line" special character like Environment.NewLine
static property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
嗯,简单的选项是:
string.Format
:字符串连接:
字符串插值(C#6 及以上版本):
您还可以在任何地方使用 \n,并替换:
请注意,您不能将其设为字符串常量,因为
Environment.NewLine
的值仅在执行时可用。Well, simple options are:
string.Format
:String concatenation:
String interpolation (in C#6 and above):
You could also use \n everywhere, and replace:
Note that you can't make this a string constant, because the value of
Environment.NewLine
will only be available at execution time.如果您想要一个包含 Environment.NewLine 的 const 字符串,您可以执行以下操作:
编辑
由于这是在 const 字符串中,因此它是在编译时完成的,因此它是编译器对换行符的解释。我似乎找不到解释这种行为的参考资料,但是,我可以证明它按预期工作。我在 Windows 和 Ubuntu(使用 Mono)上编译了此代码,然后反汇编,结果如下:
如您所见,在 Windows 中换行符被解释为 \r\n,在 Ubuntu 上被解释为 \n
If you want a const string that contains Environment.NewLine in it you can do something like this:
EDIT
Since this is in a const string it is done in compile time therefore it is the compiler's interpretation of a newline. I can't seem to find a reference explaining this behavior but, I can prove it works as intended. I compiled this code on both Windows and Ubuntu (with Mono) then disassembled and these are the results:
As you can see, in Windows newlines are interpreted as \r\n and on Ubuntu as \n
在格式字符串中方便放置 Environment.NewLine 的另一种方法。
这个想法是创建字符串扩展方法,该方法像往常一样格式化字符串,但也用 Environment.NewLine 替换文本中的 {nl}
用法
代码
注意
如果您希望 ReSharper 突出显示您的参数,请在上面的方法中添加属性
[StringFormatMethod("format")]
这种实现方式显然比 String 效率低.Format
也许,对这个问题感兴趣的人也会对下一个问题感兴趣:
C# 中的命名字符串格式
One more way of convenient placement of Environment.NewLine in format string.
The idea is to create string extension method that formats string as usual but also replaces {nl} in text with Environment.NewLine
Usage
Code
Note
If you want ReSharper to highlight your parameters, add attribute to the method above
[StringFormatMethod("format")]
This implementation is obviously less efficient than just String.Format
Maybe one, who interested in this question would be interested in the next question too:
Named string formatting in C#
如果你确实希望New Line字符串作为常量,那么你可以这样做:
C#中readonly关键字的使用者意味着这个变量只能被赋值一次。您可以在此处找到相关文档。它允许声明一个常量变量,其值在执行时才知道。
If you really want the New Line string as a constant, then you can do this:
The user of the readonly keyword in C# means that this variable can only be assigned to once. You can find the documentation on it here. It allows the declaration of a constant variable whose value isn't known until execution time.
较新的 .net 版本允许您在文字前面使用 $,这允许您在内部使用变量,如下所示:
newer .net versions allow you to use $ in front of the literal which allows you to use variables inside like follows:
如果您正在使用 Web 应用程序,您可以尝试这个。
If you are working with Web application you can try this.
如果我理解这个问题:结合
“\r\n”
以在文本框
中获取下面的新行。我的示例有效 -典型的答案可能是
s1
文本框
中的s2
使用定义的类型样式。If I understand the question: Couple
"\r\n"
to get that new line below in atextbox
. My example worked -A typical answer could be
s1
s2
in thetext box
using defined type style.我更喜欢“Pythonic方式”
I like more the "pythonic way"
在这里,Environment.NewLine 不起作用。
我将“br/>”放入字符串中并工作。
前任:
Here, Environment.NewLine doesn't worked.
I put a "<br/>" in a string and worked.
Ex: