4

我相当确定这是 JavaFX 剪贴板中的一个错误,但我想确保我没有做一些愚蠢的事情。我使用以下代码以编程方式将纯文本放在剪贴板上:

Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
//String test = "1" + System.lineSeparator() + "2"; //Example 1 - Two lines
//String test = "1\r\n2"; //Example 2 - Two lines
String test = "1\n2"; //Example 3 - One line
content.putString(test);
clipboard.setContent(content);

示例 1 和 2 粘贴后产生此文本

1

2

示例 3 粘贴后出现此文本(如预期的那样)

1
2 

使 notepad++ 显示行尾确认在前两个示例中行尾被加倍。在它上面运行调试器显示字符串在放入 ClipboardContent 后很好,但在那之后我不再关注它。

这一切都在 Windows 8 上(运行代码和粘贴操作)。我的结论是,在系统深处的某个地方,它检测到 Windows 行结尾的需要,并在粘贴发生之前将每个 \r 和 \n 转换为 \r\n。

4

1 回答 1

1

我用一个简单的 replaceAll 解决了这个问题,如下所示:

final ClipboardContent content = new ClipboardContent();
content.putString(str.replaceAll("\r\n", "\n"));
于 2015-02-09T20:21:37.243 回答