[英]Line Break using Clipboard SetText
How can I add line breaks using the SetText method? 如何使用SetText方法添加换行符?
I tried Clipboard.SetText("eee \\n xxxx");
我试过
Clipboard.SetText("eee \\n xxxx");
but it doesn't give me the expected result when I paste the clipboard data in the notepad. 但是当我将剪贴板数据粘贴到记事本中时,它并没有给我预期的结果。
Expected result: 预期结果:
eee
xxxx
How can I accomplish this? 我怎么能做到这一点?
Windows uses CR+LF pairs to indicate a new line. Windows使用CR + LF对来表示新行。 This equates to
"\\r\\n"
in C#. 这相当于C#中的
"\\r\\n"
。 However, you are just sending "\\n"
, ie a single LF rather than a CR+LF pair. 但是,您只是发送
"\\n"
,即单个LF而不是CR + LF对。
Use Environment.NewLine
rather than of "\\n"
. 使用
Environment.NewLine
而不是"\\n"
。 This is the idomatic way to spell "\\r\\n"
in C#. 这是在C#中拼写
"\\r\\n"
的惯用方法。 As a bonus, if you ever ran your code on a *nix platform, Environment.NewLine
would evaluate to "\\n" which is the *nix new line indicator. 作为奖励,如果您在* nix平台上运行代码,
Environment.NewLine
将评估为“\\ n”,这是* nix新行指示符。 Finally, in my view Environment.NewLine
is preferable from a readability perspective. 最后,在我看来,
Environment.NewLine
从可读性的角度来看是可取的。 It documents what this thing is logically rather than relying on you knowing the magic constants. 它记录了这个东西在逻辑上而不是依赖于你知道神奇的常数。
尝试:
Clipboard.SetText("eee" + Environment.NewLine + "xxxx");
我发现这也很好用:
System.Windows.Forms.Clipboard.SetText(this.txtCopyJS.Text.Replace("\n", "\r\n"));
I know this is a bit late... but I often use StringBuilder for memory and performance reasons and it' has a very handy AppendLine() method. 我知道这有点晚了......但我经常使用StringBuilder来获取内存和性能,并且它有一个非常方便的AppendLine()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.